Идентификация порядкового номера строки на основе максимального значения другого столбца в Group_by

 G= c("P52" ,"P52","P66 - PARK II","P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P82 V3","P82 V3","P82 V3","P52","P52","P66 - PARK II" ,"P66 - PARK II" ,"P82 V3","P66 - PARK II" ,"P66 - PARK II" ,"P82 V3","P82 V3"        ,"P88","P88","P88","P52","P82 V3","P66 - PARK II" ,"P66 - PARK II" ,"P82 V3","P52","P88","P88","P52" ,"P88","P82 V3","P88","P82 V3","P82 V3","P82 V3","P88","P88","P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P88","P66 - PARK II" ,"P88"           ,"P52","P52","P52","P52","P52","P52","P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II","P88","P82 V3","P82 V3","P88","P88","P66 - PARK II" ,"P66 - PARK II" ,"P52","P52","P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II","P66 - PARK II" ,"P82 V3","P66 - PARK II" ,"P82 V3","P88","P82 V3","P88","P88","P66 - PARK II","P82 V3","P82 V3" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P82 V3","P88","P88","P82 V3","P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P66 - PARK II" ,"P88"           ,"P66 - PARK II" ,"P66 - PARK II","P66 - PARK II" )
GA = runif(100, min=20, max=50)
GL = runif(100, min=20, max=50)

df=data.frame(G=G,GA=GA,GL=GL)
df$Sq <- seq(1:nrow(df))
library(MASS)
library(dplyr)
by_G<- group_by(df,G)
fg1 <- summarise(by_G,
                 Row_Index1=row_number(which.max(GA)),
                 Row_Index2=row_number(which.max(GL)),
                 n=n())  

Выше воспроизводимый код, я пытаюсь извлечь номер идентификатора строки, где столбец GA имеет максимальное значение в группе.

1 ответ

Решение

Предполагая, что Sq относится к идентификатору строки в вашем наборе данных, следующее даст идентификатор строки с наибольшим GA в каждой группе G:

by_G %>% arrange(desc(GA)) %>% summarise(Sq = first(Sq))

# A tibble: 4 × 2
              G   Sq
         <fctr> <int>
1           P52    46
2 P66 - PARK II    13
3        P82 V3    90
4           P88    55
Другие вопросы по тегам