Get row with highest value from one column after chunking with plyr - R

五迷三道 提交于 2019-12-06 08:41:11

From the previous results, I'd use [ to subset on your first condition using logical comparators and then do a second subset on your second condition because as @sgibb points out in the comments, the max value of v6 might not be in a row where v1 == 1.

#  Subset to those rows where v1 == 1
tmp <- df[ df$v1 == 1 , ]

#  Then select those rows where the max value of v6 appears
tmp[ tmp$v6 == max( tmp$v6 ) , ]

If you want the first occurence, use which.max()

we could also use the subset operator like

x_sub= subset(x, state == "C" & chainlength == 5 & segment == "C2C_REG")

where x is the data frame and the other parameter is a logical expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!