0

I try to use mutate in dplyr library but, I am having an error message, where am I doing mistake? I just try to add 1 more column as a name E, and the rule is; if Visit == BLR or F04,E is 1 or if visit == F12 or F16 E is 2 or Visit == F16 or F20 E is 3. You can see also my input data and expected output data.

inp<-data.frame(MaskID = c(10,10,10,10,12,2),Visit = c("BLR", "F04", "F16", "F20", "F12", "F20"),SBP = c(8,7,6,12,11,21))

inp %>% group_by(MaskID) %>% mutate(E = ifelse( Visit == c("BLR" | "F04"), 1,ifelse(Visit == c("F08" | "F12"), 2,3)))

out <- data.frame(MaskID = c(10,10,10,10,12,12),Visit = c("BLR", "F04", "F16", "F20", "F12", "F20"), SBP = c(8,7,6,12,11,21), E = c(1,1,3,3,2,3 ))

Melih Aran
  • 23
  • 4
  • 1
    Use `%in%` instead of `==` i.e. `Visit %in% c("BLR", "F04")`, there is a typo for `|` i.e. `inp %>% mutate(E = case_when(Visit %in% c("BLR", "F04") ~ 1, Visit %in% c("F08", "F12") ~ 2, TRUE ~ 3))` – akrun Dec 04 '21 at 21:36

0 Answers0