this is my code:
library(dplyr)
# Create dataframe
df <- data.frame(
col1 = rep(2, 10),
col2 = rep(4, 10),
col3 = rep(6, 10),
col4 = c(NA, rep(8, 9)))
#create a new variable
df <- df %>%
mutate(index = ifelse((col1 == 2) + (col2 == 4) + (col3 == 6) + (col4 == 8) >= 3, 0, 1))
According to the ifelse statements if at least 3 conditions are met, the code will assign 0 otherwise 1. The problem is that in my dataset there are missing data, and this is a bit problematic when using ifelse conditions. As in the example, the first row has 1 missing data. Despite 3 out of four conditions are satisfied, it returns an NA in the index, instead of the expected 0. Any suggestion on how can I deal with it?