dimanche 3 avril 2016

apply a function in data frame with if statement in R

I have a data frame looks like the following

data.1 <- data.frame(
  X1 = 6:10,
  X2 = 1:5,
  X3 = c(TRUE,FALSE,TRUE,FALSE,TRUE)
)

  X1 X2    X3
1  6  1  TRUE
2  7  2 FALSE
3  8  3  TRUE
4  9  4 FALSE
5 10  5  TRUE

I want to create a new column X4 with the following logic:

if X3==NULL then X4=NULL
elseif X3==TRUE then X4=X1+X2 
else X4=X1-X2

Thanks in advance

Aucun commentaire:

Enregistrer un commentaire