I'm a brand New R user, this is my first attemt at conditional statements - and first time posting an online question - so please bear with me.
I'm managing data from a questionaire survey into something analyseable. As a rookie mistake, I've been using the question form "tick all that apply" frequently - obatining a matrix with one column per alternative answer. I am trying to combine these by conditional logic into a single column per question.
Using one of these matrices as example, my dataframe BS looks like this:
ID Q29_ Q29_ Q29_3 Q29_4
1 1 NA NA 1
2 NA 1 1 1
3 1 NA NA NA
4 NA 1 NA NA
[…]
Using following code
```BS1 <- BS %>%
mutate(WaterOutdoors = c("", "Yes")[(Q29_2 %in% c("Outdoors") |
Q29_3 == "Outdoors" |
Q29_4 == "Outdoors")+1]) ```
I get
ID Q29_ Q29_ Q29_3 Q29_4 WaterOutdoors
1 1 NA NA 1 Yes
2 NA 1 1 1 Yes
3 1 NA NA NA
4 NA NA NA NA
5 NA 1 NA NA Yes
[…]
I would like to add a "No"-option to the "WaterOutdors" variable for those rows who have ticked off Q29_1 (Q29_1="1") and did not tick of any of the following (Q29_2=NA, Q29_3=NA, Q29_4=NA), and at the same time leave "WaterOutdoors" either empty or NA if none of the Q29_1:_4 have been ticked of.
I have tried
```BS1$WaterOutdoors <- with(BS1, ifelse(Q29_1 %>% c("InStable") &
is.na(Q29_2) &
is.na(Q29_3) &
is.na(Q29_4),
"No","")) ```
hoping to get
ID Q29_ Q29_ Q29_3 Q29_4 WaterOutdoors
1 1 NA NA 1 Yes
2 NA 1 1 1 Yes
3 1 NA NA NA No
4 NA NA NA NA NA
5 NA 1 NA NA Yes
[…]
but rather got the error-message: "Error in Q29_1 %>% c("InStable") & is.na(Q29_2) : operations are possible only for numeric, logical or complex types"
My logic is obviously flawed, any input on how I should have gone about doing this would be greatly appreciated!
Aucun commentaire:
Enregistrer un commentaire