Subset data based on Minimum Value

旧巷老猫 提交于 2019-11-29 17:19:55

We can subset the rows with which.min. After grouping with 'ID', we slice the rows based on the position of minimum 'Distance'.

library(dplyr)
dat %>% 
   group_by(ID) %>% 
   slice(which.min(Distance))

A similar option using data.table would be

library(data.table)
setDT(dat)[, .SD[which.min(Distance)], by = ID]

If you prefer ddply you could do this

library(plyr)
ddply(dat, .(ID), summarize, 
      Seg = Seg[which.min(Distance)], 
      Distance = min(Distance))

#    ID    Seg  Distance
#1 V147 Seg159  14.74852
#2 V171 Seg233 193.01636
#3  V21  Seg46 160.37672
#4  V85 Seg373 167.38930
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!