0

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)

I am unable to get column B Content when Column-C has Max Count and grouping by Column A

Nabi Shaikh
  • 787
  • 1
  • 6
  • 26

1 Answers1

1

After grouping by 'A', we can use which.max in slice to get the rows that corresponds to the max value in 'C'

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

data

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")
akrun
  • 874,273
  • 37
  • 540
  • 662