跨列使用 case_when 创建新列

J.S*_*ree 2 string r case-when conditional-statements dplyr

我有一个大型数据集,其中包含许多带有状态的列。我想创建一个包含参与者当前状态的新专栏。我正在尝试在 dplyr 中使用 case_when,但我不确定如何跨列。数据集的列太多,我无法输入每一列。以下是数据示例:

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"))
Run Code Online (Sandbox Code Playgroud)

对于代码,我想要一个新列,说明参与者的最终状态;但是,如果他们的地位不断被完成,那么我想它说完成,无论其最终状态是什么。对于此数据,答案如下所示:


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"))
Run Code Online (Sandbox Code Playgroud)

另外,如果您能对您的代码进行任何解释,我将不胜感激!如果您的解决方案也可以使用 contains("status"),那将特别有用,因为在我的真实数据集中,状态列非常混乱(即 summary_status_5292019、sum_status_07012018 等)。

谢谢!

akr*_*run 5

一个选项 pmap

library(tidyverse)
problem %>%
     mutate(finalstatus =  pmap_chr(select(., starts_with('status')), ~ 
       case_when(any(c(...) == "completed")~ "completed",
             any(c(...) == "withdrawn") ~ "no action", 
     TRUE ~ "registered")))
Run Code Online (Sandbox Code Playgroud)