I have tried the dplyr solution in R Set Column Value based on other Column Values, but when I run it I get the columns all repeated as THING$THING1 etc when I need just one column that flags as 1, when any of the columns THING1:THING4 contain a 1, and 0 if none of them do. (The data is using 1 for yes as the answer to a series of related questions, and 0 for no.)
| Thing1 | Thing2 | Thing3 | Thing4 |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 |
| 0 | 0 | 0 | 0 |
| 0 | 0 | 1 | 0 |
| 1 | 0 | 0 | 0 |
| 0 | 0 | 0 | 0 |
And I want the to get the column:
| Thing |
|---|
| 0 |
| 1 |
| 1 |
| 0 |
| 1 |
| 1 |
| 0 |
The code as I'm using it is:
Things <- dataset %>%
select(c(THING1:THING4)) %>%
mutate(THING = across(.cols = THING1:THING4,
.fns = ~ if_else(.x == 1|is.na(.x),
1,
0)))
I am using a vector as the real data has about a dozen columns to check.