I have a large dataset that has many columns with status. I want to make a new column that has the participants' current status. I'm trying to use case_when in dplyr, but I'm unsure how to go across columns. The dataset has too many columns for me to type out each column. Here's a sample of the data:
library(dplyr)
problem <- tibble(name = c("sally", "jane", "austin", "mike"),
status1 = c("registered", "completed", "registered", "no action"),
status2 = c("completed", "completed", "registered", "no action"),
status3 = c("completed", "completed", "withdrawn", "no action"),
status4 = c("withdrawn", "completed", "no action", "registered"))
For the code, I want a new column that says the participants' final status; HOWEVER, if their status ever was completed, then I want it to say completed, regardless of what their final status is. For this data, the answer would look like this:
answer <- tibble(name = c("sally", "jane", "austin", "mike"),
status1 = c("registered", "completed", "registered", "no action"),
status2 = c("completed", "completed", "registered", "no action"),
status3 = c("completed", "completed", "withdrawn", "no action"),
status4 = c("withdrawn", "completed", "no action", "registered"),
finalstatus = c("completed", "completed", "no action", "registered"))
Also, if you can include any explanation of your code, I would really appreciate it! If your solution could also use the contains("status"), that would be especially helpful because in my real data set, the status columns are very messy (i.e. summary_status_5292019, sum_status_07012018, etc).
Thank you!