Subset data based on Minimum Value

后端 未结 2 1953
清酒与你
清酒与你 2020-12-21 13:32

This might an easy one. Here\'s the data:

dat <- read.table(header=TRUE, text=\"
Seg  ID  Distance
Seg46      V21 160.37672
Seg72      V85 191.24400
Seg37         


        
2条回答
  •  鱼传尺愫
    2020-12-21 14:39

    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
    

提交回复
热议问题