r - Identifying Row Index Number on the Bases of Max Value of Other Column within 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())
above reproducible code , trying extract row id number column ga
has max value within group.
assuming sq refers row id in dataset, following give row id largest ga within each group g:
by_g %>% arrange(desc(ga)) %>% summarise(sq = first(sq)) # tibble: 4 × 2 g sq <fctr> <int> 1 p52 46 2 p66 - park ii 13 3 p82 v3 90 4 p88 55
Comments
Post a Comment