This question is slightly similar to this question with a more theoretical component.
Given df below:
varA <- c(1,0,0,NA,NA)
varB <- c(NA,NA,NA,1,0)
df <- data.frame(varA, varB)
varA varB
1 NA
0 NA
0 NA
NA 1
NA 0
What's the most elegant method to generate var (with consideration given to NA) which combines the information from varA and varB?
varA varB var
1 NA 1
0 NA 0
0 NA 0
NA 1 1
NA 0 0
My approach, right now, is as follows:
df$var[df$varA == 1 | df$varB == 1] <- 1
df$var[df$varA == 0 | df$varB == 0] <- 0
As a side question, how does R handle NA in ifelse statements? For example, if I write the following code, it does not produce the output I intended.
df$var <- ifelse(df$varA == 1 | df$varB == 1, 1,
ifelse(df$varA == 0 | df$varB == 0, 0, NA)
Aucun commentaire:
Enregistrer un commentaire