I have a df data.frame that consists of 4 variables a, b, c and d and 100 observations. There are missing values in variables b and c
set.seed(123)
a <- runif(100, 0.005, 2.3)
b <- runif(100, 0.2, 4.3)
c <- runif(100, 0.03, 1.6)
d <- runif(100, 0.05, 2.4)
df <- data.frame(a, b, c, d)
df[c(1:4,8:10, 30:35, 60:65,90:97), c(2,3)]<-NA
head(df)
# a b c d
#1 0.6649904 NA NA 1.89375188
#2 1.8141603 NA NA 0.07216028
#3 0.9436020 NA NA 1.88080482
#4 2.0315249 NA NA 1.76406803
#5 2.1633724 2.179900 0.6620401 1.53080985
#6 0.1095522 3.850436 1.4119871 1.18014045
I want to create column E in which
if c = NA ==> E = a+d else E= a + b + c
I tried doing it using ifelse as below
df$E <- 0
df$E <- ifelse(df$c == NA,
df$a + df$d,
df$a+df$b+df$c)
The code runs but the E column was filled by NA. Any suggestions would be appreciated?
Aucun commentaire:
Enregistrer un commentaire