【发布时间】:2019-10-31 03:31:12
【问题描述】:
我有这个数据集:
library(dpylr)
Problem<- tibble(name = c("Angela", "Claire", "Justin", "Bob", "Gil"),
status_1 = c("Registered", "No Action", "Completed", "Denied", "No Action"),
status_2 = c("Withdrawn", "No Action", "Registered", "No Action", "Exempt"),
status_3 = c("No Action", "Registered", "Withdrawn", "No Action", "No Action"))
我想创建一个包含每个人当前状态的专栏。如果这个人曾经完成过课程,他们就完成了。如果他们曾经被豁免,他们就会被排除在外。如果它们不是已注册(或已完成或豁免)的其他任何内容,则它们是“未采用”。困难的是我希望我的代码说他们只有在他们的最后一个动作被注册时才被注册。所以,它应该是这样的:
library(dplyr)
solution <- tibble(name = c("Angela", "Claire", "Justin", "Bob", "Gil"),
status_1 = c("Registered", "No Action", "Completed", "Denied", "No Action"),
status_2 = c("Withdrawn", "No Action", "Registered", "No Action", "Exempt"),
status_3 = c("No Action", "Registered", "Withdrawn", "No Action", "No Action"),
current = c("Not Taken", "Registered", "Completed", "Not Taken", "Exempt")
我有这段代码,不起作用的部分是 which.max() 行:
library(dplyr)
library(purrr)
library(stringr)
problem %>%
mutate(
current =
pmap_chr(select(., contains("status")), ~
case_when(
any(str_detect(c(...), "(?i)Completed")) ~ "Completed",
any(str_detect(c(...), "(?i)Exempt")) | any(str_detect(c(...), "(?i)Incomplete")) ~ "Exclude",
which.max(parse_number(colnames(.)) == "Registered") ~ "Registered",
any(str_detect(c(...), "(?i)No Show")) | any(str_detect(c(...), "(?i)Denied")) | any(str_detect(c(...), "(?i)Cancelled")) | any(str_detect(c(...), "(?i)Waitlist Expired")) || any(str_detect(c(...), "(?i)Withdrawn")) ~ "Not Taken",
TRUE ~ "NA"
)
)
)
我已经尝试了 R 读取状态数字的各种方法,但我无法弄清楚。保留其余代码很重要,尤其是 str_detect() 部分,因为虽然我的示例数据是干净的,但真实数据集有许多状态行和许多看起来像“COMPLETED”和“completed”的条目。
为什么我不能查看带有解析号的 purrr 以使其读取最大状态?
谢谢!
【问题讨论】:
标签: r dplyr conditional-statements purrr stringr