Hi, I am having a hard time re-coding values in a data frame based on conditional logic using more than one column. Here is what I mean from an example. Here we have two columns about a schedule.
The schedule has to be both requested and approved, such that we have schedules that are both requested and approved and schedules that were requested but not approved. I want to capture whether schedules were either not requested, requested and approved, or requested but not approved. How could I capture this conditional logic into one column?
Here is my solution that I bet someone here could improve on Basically I concatenated the two columns together and then re-coded them to indicate the possibilities.
schedule <- data.frame(requested = c("Yes","No","Yes","Yes","Yes"), approved = c("Yes",NA,"No","Yes","Yes")) schedule
requested approved 1 Yes Yes 2 No <NA> 3 Yes No 4 Yes Yes 5 Yes Yes
schedule$requestapprove <- paste0(schedule$requested, schedule$approved) library(plyr) schedule$requestapprove.r <- revalue(schedule$requestapprove, c("YesYes" = "Approved", "NoNA" = "Not requested", "YesNo" = "Requested but not approved")) schedule
requested approved requestapprove requestapprove.r 1 Yes Yes YesYes Approved 2 No <NA> NoNA Not requested 3 Yes No YesNo Requested but not approved 4 Yes Yes YesYes Approved 5 Yes Yes YesYes Approved