R, conditionally remove duplicate rows

前端 未结 3 693
天涯浪人
天涯浪人 2020-12-09 20:17

I have a dataframe in R containing the columns ID.A, ID.B and DISTANCE, where distance represents the distance between ID.A and ID.B. For each value (1->n) of ID.A, there ma

3条回答
  •  悲&欢浪女
    2020-12-09 21:15

    You can also do it easily in base R. If dat is your dataframe,

    do.call(rbind, 
            by(dat, INDICES=list(dat$ID.A), 
               FUN=function(x) head(x[order(x$DISTANCE), ], 1)))
    

提交回复
热议问题