I have a simple question but it is giving me a hard time. I wish to recode a varibale with a ifelse function but I want to keep the NAs (in the variable "party") as NAs.
This is what I am doing. I have a variable with vote choice ("party") and I want to recode a new variable ("RRP") if participants choose a specific set of parties:
df<- df %>%
mutate(RRP = if_else(party %in% c("4",#SPP/UDC
"12",#Swiss Democrats
"13",#FDU
"14", #PSL
"15"), 1, 0))
The problem is that participants who have NAs in the "party" variable are now being recoded as 0 because they don't fullfil the condition in ifelse.
I also tried this:
df<- df %>%
mutate(RRP = if_else(party %in% c("4",#SPP/UDC
"12",#Swiss Democrats
"13",#FDU
"14", #PSL
"15"), 1, 0, na.rm=TRUE))
but it just yield an error. Do you have any other ideas?
Thanks a lot!
Aucun commentaire:
Enregistrer un commentaire