Home > Blockchain >  keep each value in the biggest group - R
keep each value in the biggest group - R

Time:12-19

I have df with groups A,B,C,D for each group there is rank A 1 the higher, B , c then D the lowest.

in each group, there is Names and ID I need to keep for each one the highest ranks only.

ID | Name | Group
101| Jon  | A
101| Jon  | C
103| Tom  | B
103| Tom  | D
103| Tom  | C

the output needs like that

ID | Name | Group
101| Jon  | A
103| Tom  | B

What is the best way to do it using Tidyverse R

Thanks

I already use distinct to keep one value for each ID, I used case_when to keep in each group. but I now want to keep only the higher rank.

CodePudding user response:

We may use

library(dplyr)
df1 %>%
 arrange(ID, Name, factor(Group, levels = c("A", "B", "C", "D"))) %>%
 distinct(ID, .keep_all = TRUE)

-output

   ID Name Group
1 101  Jon     A
2 103  Tom     B

data

df1 <- structure(list(ID = c(101, 101, 103, 103, 103), Name = c("Jon", 
"Jon", "Tom", "Tom", "Tom"), Group = c("A", "C", "B", "D", "C"
)), class = "data.frame", row.names = c(NA, -5L))
  • Related