Remove duplicates based on 2nd column condition

前端 未结 4 752
野的像风
野的像风 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:22

    Using base R. Here, the columns are factors. Make sure to convert it to numeric

     df$val2 <- as.numeric(as.character(df$val2))
     df[with(df, ave(val2, id, FUN=max)==val2),]
     #  id val1 val2
     #3  a    3    5
     #5  b    2    6
     #6  r    4    5
    

    Or using dplyr

     library(dplyr)
     df %>% 
        group_by(id) %>% 
        filter(val2==max(val2))
     #   id val1 val2
     #1  a    3    5
     #2  b    2    6
     #3  r    4    5
    

提交回复
热议问题