Home > database >  Nested ifelse() statement in R not producing the desired results
Nested ifelse() statement in R not producing the desired results

Time:03-17

I have a data set of boolean variables and I am trying to generate a new variable based on 3 of the existing booleans using ifelse().

The rules I'd like to implement are:

  1. If any of the three columns have value 1, 1
  2. If all of the three columns have value 0, 0
  3. If all of the three columns have value NA, NA
  4. If the three columns have some combination of 0 and NA, 0

Here is the code to generate a sample with 3 variables that I want to use to create a fourth:

df <- structure(list(var1 = c(NA, NA, NA, 0,1), 
                             var2 = c(1, NA, 0,0, 1), 
                             var3 = c(NA, NA, NA,0,1)), class = "data.frame", row.names = c(NA, -5L))
        

I have tried the following to generate the new variable according to my desired rules:

df$newvar1 <-ifelse(df$var1 == 1 | df$var2 == 1 |df$var3 == 1, 1, 
                     ifelse((is.na(df$var1) & is.na(df$var2) & is.na(df$var3)), NA,0))
                             
df$newvar2 <- ifelse((is.na(df$var1)|df$var1==0) & 
                     (is.na(df$var2)|df$var2==0) & 
                     (is.na(df$var3)|df$var3==0),0,
                     ifelse(df$var1 == 1 | df$var2 == 1 |df$var3 == 1, 1, 
                           ifelse(is.na(df$var1) & is.na(df$var2) & is.na(df$var3), NA,NA)))
        

df$newvar3 <-ifelse(df$var1 == 1 | df$var2 == 1 |df$var3 == 1, 1, 
                     ifelse((is.na(df$var1) & is.na(df$var2) & is.na(df$var3)), NA,
                            ifelse((is.na(df$var1)|df$var1==0) & 
                              (is.na(df$var2)|df$var2==0) & 
                              (is.na(df$var3)|df$var3==0),0,0)))


I don't understand why newvar1 and newvar3 have NA values corresponding to combinations of NAs and 0s when both examples use "&" between the na specifications (row 3 in the results).

I am assuming that NAs don't show up in newvar2 because the first ifelse() function takes precedent.

Any insight to the ifelse() function or advice on how to get the results I'm looking for would be really helpful.

Results

CodePudding user response:

Here is another possible option using rowSums:

df$newvar <-  (rowSums(df, na.rm = TRUE) * NA ^ (rowSums(!is.na(df)) == 0) > 0)

#  var1 var2 var3 newvar
#1   NA    1   NA      1
#2   NA   NA   NA     NA
#3   NA    0   NA      0
#4    0    0    0      0
#5    1    1    1      1

CodePudding user response:

This gives your expected results:

df$newvar <- 0
df$newvar[Reduce(`|`, lapply(df[1:3], `%in%`, 1))] <- 1
df$newvar[Reduce(`&`, lapply(df[1:3], is.na))] <- NA
df
#   var1 var2 var3 newvar
# 1   NA    1   NA      1
# 2   NA   NA   NA     NA
# 3   NA    0   NA      0
# 4    0    0    0      0
# 5    1    1    1      1

This defaults to 0 and only changes values with known conditions, which means that if there are any rows with NA and 1 (with or without 0), it will be assigned 0. It's not difficult to test for this, but it wasn't in your logic.

  • Related