Might be a very basic question. However (maybe as I am quite new to R) I could not find a smarter solution for my little issue with case_when
.
If I have more than one value that matches the condition of ONE case to be true, one option is the following syntax:
> testnr <- 1
> print(case_when(testnr == 2 | testnr == 3 | testnr == 7 | testnr == 9 ~ "true", TRUE ~ "false"))
[1] "false"
> print(case_when(testnr == 1 | testnr == 3 | testnr == 7 | testnr == 9 ~ "true", TRUE ~ "false"))
[1] "true"
So far, so good. But, especcially when having some more 'true'-values (instead of just four as in my example), code is gonna be very long. So, I'm looking for shorter way in the sense of if testnr == 2 or 3 or ...
without repeating testnr ==
between each |
Here some of my 'genius' ideas which all went wrong:
> print(case_when(testnr == 2 | 3 | 7 | 9 ~ "true", TRUE ~ "false"))
[1] "true"
> print(case_when(testnr == (2 | 3 | 7 | 9) ~ "true", TRUE ~ "false"))
[1] "true"
> print(case_when(testnr == c(2 | 3 | 7 | 9) ~ "true", TRUE ~ "false"))
[1] "true"
> print(case_when(testnr == c(2,3,7,9) ~ "true", TRUE ~ "false"))
[1] "false" "false" "false" "false"
Any body an idea for me?
And in case there should not be any smarter alternative I would also be glad if somebody would confirm that.