lundi 9 avril 2018

if-else condition based on a data frame's columns

I need to create a new column for the following data frame based on if-else condition.

 adminn_cleaned adminn_needs_update adminn_updated
           Sean                TRUE           Mike
           Sean               FALSE               

I need a new column "adminn_final," where if adminn_needs_update is TRUE, adminn_final would equal adminn_updated. Otherwise, adminn_final would be adminn_cleaned. So if I can create the column 'adminn_final', my new data frame would look as follows:

 adminn_cleaned adminn_needs_update adminn_updated adminn_final
           Sean                TRUE           Mike Mike
           Sean               FALSE                Sean

I tried the following code but the adminn_final column contains numbers, not the expected names.

testing2 <- data.frame(adminn_cleaned=c("Sean","Sean"), adminn_needs_update=c(TRUE,FALSE), adminn_updated=c("Mike",""))

testing2$adminn_final <- with(testing2, ifelse (adminn_needs_update==TRUE, adminn_updated, adminn_cleaned))

Aucun commentaire:

Enregistrer un commentaire