使用DPLyr基于分组[A]和最大(计数)[c]选择特定列[B]的内容

时间:2016-12-16 04:38:07

标签: r dplyr

As we can see in Figure we have **column A,B,C**.So my Query is i want Group by **Column-A** and Select the Content of **Column-B** depending on Maximum Count of **Column-C**

by_g <- (df, A)    
df.new <- by_g %>%
        summarise(v <- max(C))%>% arrange(A)

当Column-C具有最大计数并按列A分组时,我无法获得B列内容

1 个答案:

答案 0 :(得分:1)

按“A”分组后,我们可以使用which.max中的slice来获取与“C”中的max值对应的行

df %>%
   group_by(A) %>%
   slice(which.max(C)) 

数据

df <- structure(list(A = c("B001", "B001", "B001", "B001", "B002", 
"B002", "B002", "B003", "B003", "B003", "B003", "B003", "B003"
), B = c("", "Elec VoltageAsymmetry", "Pitch LubricationStop", 
"Pitch SafetyTestActiv", "", "Elec VoltageAsymmetry", "Pitch LubricationStop", 
"", "Elec CurrentAsymmetry", "Elec VectorSurgeStop", "Elec VoltageAsymmetry", 
"Pitch FreqConvPitch2 ErrStop", "Pitch LubricationStop"), C = c(1, 
1, 5, 1, 1, 1, 5, 1, 2, 2, 2, 1, 4)), .Names = c("A", "B", "C"
), row.names = c(NA, -13L), class = "data.frame")