0

Can someone tell me what is wrong with this chunk of code:

df %>% mutate(age2=case_when(
    age %in% 0:20 ~ "A"
    age %in% 20:40 ~ "B"
    age %in% 40:60 ~ "C"
    age %in% 60:80 ~ "D"
    age %in% 80:100 ~ "E"
     T ~ ""))

I get error saying: Error: unexpected symbol in: "age %in% 0:20 ~ "A" I believe ~ is the problem, but not sure how to solve it.

Thank you

www
  • 38,575
  • 12
  • 48
  • 84
  • Not sure what happened since you did not provide a reproducible example, but if you are working on numeric data, would you like to try `age > 0 & age <= 20`? It seems to me that the way you assign values could be problematic, not clear if `20` should be in "A" or "B", for example. – www Dec 06 '17 at 18:00
  • 3
    See also `cut` (e.g., [this R-FAQ](https://stackoverflow.com/q/5570293/903061)). `age2 = cut(age, breaks = c(-Inf, seq(20.5, 100.5, by = 20)), labels = LETTERS[1:5])` – Gregor Thomas Dec 06 '17 at 18:21
  • oh my god thank you so much for cut!I did not know about this at all – Bioinformatician Dec 06 '17 at 18:29
  • 1
    Thank you @www..your comment started the discussion and I got more than what i wanted :) – Bioinformatician Dec 06 '17 at 18:30

1 Answers1

2

You need commas between the cases (source):

df %>% mutate(age2=case_when(
    age %in% 0:20 ~ "A",
    age %in% 20:40 ~ "B",
    age %in% 40:60 ~ "C",
    age %in% 60:80 ~ "D",
    age %in% 80:100 ~ "E",
     T ~ ""))

However, you also might want to think about www's comment in the case that age might take a non-integer value.

duckmayr
  • 16,303
  • 3
  • 35
  • 53
  • @Bioinformatician -- happens to everyone; I'm pretty good at catching it in others' code, but of course not my own... – duckmayr Dec 06 '17 at 18:31