Bear with me, since I am a beginner at both R and coding. It might be them I am to much of a newbie to understand solutions already presented, but I have tried searching the forum and have not been able to solve the issue.
I have a dataset containing two variables, var1 and var2. Some cases have a value in var1 and some in var2. Cases with a value in var1 are NA in var2 and vice versa. I would like to combine these variables into one with the value 1 or 0. The new variable should take on the value of 1 if var1 OR var2 has a value of 1, the value of 0 if var1 or var2 has a value of 0 and NA only if both var1 and var2 is NA.
Basically, what I am trying to do is to get R to ignore NA in var1 or var2 if there is a value in the other, and only return NA is NA are present in BOTH variables.
I have tried the following solutions:
First attempt:
df$new_var<-ifelse(df$var1==1|df$var2==1,1,
ifelse(df$var1<1|df$var2<1,0,
ifelse(is.na(df$var1)&df$var2<1,0,0
ifelse(is.na(df$var2)&df$var1<1,0,0))))
Second attempt:
df$new_var<-ifelse(df$var1==1|df$var2==1,1,
ifelse(df$var1==0|df$var2==0,0,NA))
Third attempt:
df$new_var<-ifelse(df$var1==1|df$var2==1,1,
ifelse(df$var1==0|df$var2==0,0,NA))
All returns a variable with 1 and NAs and no 0.
What I am doing wrong?
Thanks in advance for any input!
Aucun commentaire:
Enregistrer un commentaire