Remove duplicates based on 2nd column condition

前端 未结 4 738
野的像风
野的像风 2020-12-02 00:17

I am trying to remove duplicate rows from a data frame based on the max value on a different column

So, for the data frame:

df<-data.frame (rbind(         


        
4条回答
  •  离开以前
    2020-12-02 00:40

    One possible way is to use data.table

    library(data.table)
    setDT(df)[, .SD[which.max(val2)], by = id]
    ##    id val1 val2
    ## 1:  a    3    5
    ## 2:  b    2    6
    ## 3:  r    4    5
    

提交回复
热议问题