Reputation: 1002
Lets say I have following 2 variables
z1=c(0,1,1,0,NA,1,0,NA ,1)
z2=c(0,1,0,NA,1,0,0,0,NA)
So the both variables has missing values. I need to create a new variable Z as follows,
Z=1 if at least one of the variables (z1 and z2) are equal to 1.(following situations z1=1 & z2=1 , z1=0 & z2=1 ,z1=1 & z2=0 , z1=1 & z2=NA , z1=NA & z2=1)
Z=0 if at least one of the variables(z1 and z2) are equal to 0.(following situations z1=0 & z2=0 , z1=0 & z2=NA ,z1=NA & z2=0 )
So i performed the following nested ifelse statement and got this output,
ifelse(z1==1|z2==1,1,ifelse((z1==0|z2==0),0 ,0))
0 1 1 NA 1 1 0 NA 1
there seems to be something wrong with my if-else statement as my ideal output should be something like this,
0 1 1 0 1 1 0 0 1
can any one help me to figure out what is incorrect in my code ?
Thank you
Upvotes: 1
Views: 292
Reputation: 1438
I believe coalesce()
in the dplyr
packages accomplishes your desired output:
library(dplyr)
coalesce(z1, z2)
[1] 0 1 1 0 1 1 0 0 1
If you are looking to perhaps append the new values to your previous:
df <- tibble(
z1 = c(0, 1, 1, 0, NA, 1, 0, NA, 1),
z2 = c(0, 1, 0, NA, 1, 0, 0, 0, NA)
)
df %>%
mutate(z3 = coalesce(z1, z2))
# A tibble: 9 x 3
z1 z2 z3
<dbl> <dbl> <dbl>
1 0 0 0
2 1 1 1
3 1 0 1
4 0 NA 0
5 NA 1 1
6 1 0 1
7 0 0 0
8 NA 0 0
9 1 NA 1
Upvotes: 1
Reputation: 886938
We can use pmax
with na.rm
as TRUE
pmax(z1, z2, na.rm = TRUE)
#[1] 0 1 1 0 1 1 0 0 1
Upvotes: 3