purrr pmap通过列名编号读取最大列名

时间:2019-06-17 14:05:55

标签: r dplyr conditional-statements purrr stringr

我有这个数据集:

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()部分,因为尽管示例数据是干净的,但实际数据集具有许多状态行和许多看起来像“已完成”和“已完成”的条目。

为什么我不能用具有解析号的purrr来使它读取最大状态?

谢谢!

1 个答案:

答案 0 :(得分:1)

保持一切正常,仅处理您的/*This doesn't work: Rendering {tokenName} causes the Editor's focus to jump backwards when the new <span> gets inserted */ const TokenSpan = (props) => { let tokenGuid = props.decoratedText; let tokenName = getTokenNameById(tokenGuid); return ( <span className={styles.token} data-offset-key={props.offsetKey} > {tokenName} </span> ); }; /* This does work, but I don't want to render {props.children} - how do I do this?? */ const TokenSpan = (props) => { return ( <span className={styles.token} data-offset-key={props.offsetKey} > {props.children} </span> ); }; 问题,我们可以做到

which.max