jeudi 16 juillet 2020

NA values as conditions on a IF statement in R

My goal is to categorize the rows on my dataset depending on the values of two different dates.

 if(!exists(MY_DATA$Date_1)&exists(MY_DATA$Date_2)){
  MY_DATA$NEW_COL <- c("Category_1")
} else {
  MY_DATA$NEW_COL <- c("Category_2")
}

But it isnt working, Im currently trying a simplified version as follows

 if(!exists(MY_DATA$Date_1)){
  MY_DATA$NEW_COL <- c("Category_1")
}

however it seems that this only reads the value on the first row, and it either gives me a column with all values as "Category_1" or no column at all.

Also I have tried this with is.na() , is.null() and exists()

Aucun commentaire:

Enregistrer un commentaire