In R, trying to implement the following filtering logic by group on a large dataset:
Within each group:
If more than one L, keep the lowest L.
If more than one N, keep the highest N.
If both L and N, keep the highest value of N below the lowest L (in addition to lowest L).
Keep all B.
sample data:
dat <- data.frame(group=c("AB","AB","AB","AB","BC","BC","B","B","AD","AD","AD","G"),
type=c("B","L","N","N","N","L","N","N","B","L","L","L"),
value=c(2,4,3,2,5,3,8,9,4,3,9,7))
desired output:
desired_output <- data.frame(group=c("AB","AB","AB","BC","B","AD","AD","G"),
type=c("B","L","N","L","N","B","L","L"),
value=c(2,4,3,3,9,4,3,7))
Looking for a dplyr/tidyr solution. I’ve tried filtering logic after pivot_wider or case_when within filter, but I haven’t even gotten close enough to show any examples. I was expecting this to be simple but applying the logic across columns has me stumped.
user24835701 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1