R: Remove duplicates from a dataframe based on categories in a column

前端 未结 7 1181
耶瑟儿~
耶瑟儿~ 2021-02-15 16:14

Here is my example data set:

      Name Course Cateory
 1: Jason     ML      PT
 2: Jason     ML      DI
 3: Jason     ML      GT
 4: Jason     ML      SY
 5: Ja         


        
7条回答
  •  被撕碎了的回忆
    2021-02-15 16:29

    I would suggest using the dplyr package for this

    See below:

    require(dplyr)
    
    data %>% 
      mutate(
        Category_factored=as.numeric(factor(Category,levels=c('PT','DI','GT','SY'),labels=1:4))
      ) %>% 
      group_by(Name,Course) %>% 
      filter(
        Category_factored == min(Category_factored)
      )
    

    In case you are new to R, install dplyr using install.packages('dplyr')

提交回复
热议问题