2017-05-04 104 views
0

我的問題是關於Extract the maximum value within each group in a dataframe在R數據框的每個組(C列)內給出列B中的最大值,提取列A中的值?

  • 問題的根本在於:如何根據同一數據框中單獨列中的重複組選擇最大值?

  • 在爲如何完成這個任務後,用戶EDI provides a ton of examples

我的問題:我如何完成相同的任務,但是,而不是報告的最大值,而不是我在與該最大值相關聯的第三列報告的價值?

例如:

  • 假設我有一個data.frame:

    Group Value Year 
    A  12  1933 
    A  10  2010 
    B  3  1935 
    B  5  1978 
    B  6  2011 
    C  1  1954 
    D  3  1933 
    D  4  1978 
    
  • 對於我的分組變量的每個層面,我想提取的最高價值發生。因此,該結果應該是一個數據幀,每個分組變量的水平一行:

    Group Year 
    A  1933 
    B  2011 
    C  1954 
    D  1978 
    

我知道我可以使用任何的答案,從上面提到的電子數據交換的帖子,然後只用類似whichmatchsapply找出年份,但這似乎太渺茫。

是否有一種快速的方法來提取列A中的值,給定數據框中每個組(列C)中B列的最大值?

更新:可能有人請提供基礎R解決方案?

+0

請說明您downvote。這個問題寫得相當好,它提供了一個例子,它是關於主題的,它展示了以前的努力和搜索,並且恰如其分地遵循了規則(即,在評論中提出了一個新問題,並提出了後續問題)。是什麼賦予了?! – theforestecologist

+1

添加基礎R解決方案以回答下面。 –

+0

謝謝,@KristofferWintherBalling – theforestecologist

回答

1

這裏是一個基礎R和data.table解決方案:

df <- structure(list(Group = c("A", "A", "B", "B", "B", "C", "D", "D" 
), Value = c(12L, 10L, 3L, 5L, 6L, 1L, 3L, 4L), Year = c(1933L, 
2010L, 1935L, 1978L, 2011L, 1954L, 1933L, 1978L)), .Names = c("Group", 
"Value", "Year"), row.names = c(NA, -8L), class = "data.frame") 

# Base R - use aggregate to get max Value per group, then merge with df 
merge(df, aggregate(Value ~ Group, df, max), by.all = c("Group", "Value"))[ 
    , c("Group", "Year")] 

# Group Year 
# 1  A 1933 
# 2  B 2011 
# 3  C 1954 
# 4  D 1978 

# data.table solution 
library(data.table) 
dt <- data.table(df) 
dt[, .SD[which.max(Value), .(Year)], by = Group] 

# Group Year 
# 1:  A 1933 
# 2:  B 2011 
# 3:  C 1954 
# 4:  D 1978 
2
library(dplyr) 
df %>% group_by(Group) %>% slice(which.max(Value)) %>% select(-Value) 

#Source: local data frame [4 x 2] 
#Groups: Group [4] 

# Group Year 
# <fctr> <int> 
#1  A 1933 
#2  B 2011 
#3  C 1954 
#4  D 1978 

請注意,如果綁定存在,則每個組只能保留一個最大值。


是不斷並列最高值的方法:

library(dplyr) 
df %>% group_by(Group) %>% filter(Value == max(Value)) %>% select(-Value) 

#Source: local data frame [4 x 2] 
#Groups: Group [4] 

# Group Year 
# <fctr> <int> 
#1  A 1933 
#2  B 2011 
#3  C 1954 
#4  D 1978 
相關問題