I have a data frame (x). As you could see, I have different values (bp: yes/no and N) for the same ID, but sometimes, I have a unique ID with always bp ==no.
x
ID bp N
8012199 no 75
8012199 yes 2
8012211 no 118
8012211 yes 2
8012321 no 37
8012341 no 146
8012342 no 84
I would like to create a new variable (frequency: low and high)
x
ID bp N frequency
8012199 no 75
8012199 yes 2 low
8012211 no 118
8012211 yes 4 high
8012321 no 37 low
8012341 no 146 low
8012342 no 84 low
based on different conditions:
- if bp == yes | n < 2 :low
- if bp == yes | n > 2 :high
- if ID is unique (always = no): low
What I've tried so far is
r <- x %>%
mutate(bp= case_when(
duplicated(ID)==FALSE ~ "low",
bp %in% "yes" & n <=2 ~ "low",
bp %in% "yes" & n > 2 ~ "high" ))
duplicated(x$ID)
FALSE TRUE FALSE TRUE FALSE FALSE FALSE
It works effectively for the "yes" condition, but not for the duplication. Any advice would be fantastic!