【问题标题】:purrr pmap to read max column name by column name numberpurrr pmap 按列名编号读取最大列名
【发布时间】: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


    【解决方案1】:

    保持一切不变,只处理您的which.max 问题,我们可以做

    library(tidyverse)
    
    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(c(...) == "Registered") == length(c(...)) ~ "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"
                 )
                )
           )
    
    # name   status_1   status_2   status_3   current   
    #  <chr>  <chr>      <chr>      <chr>      <chr>     
    #1 Angela Registered Withdrawn  No Action  Not Taken 
    #2 Claire No Action  No Action  Registered Registered
    #3 Justin Completed  Registered Withdrawn  Completed 
    #4 Bob    Denied     No Action  No Action  Not Taken 
    #5 Gil    No Action  Exempt     No Action  Exempt  
    

    【讨论】:

    • 谢谢!如果您有时间,您能否口头解释一下您将如何阅读 which.max 行以及为什么它不能仅使用一个句号?再次感谢您!
    • @J.Sabree 在您的尝试中,您将列名与"Registered" 进行比较,而您需要将列的值与"Registered" 进行比较,获取值为@987654325 的最大索引@ 并检查它是否与传递的总列数相同。 (length(...))。
    • 我的真实数据集遇到了我的样本中没有的问题。有没有办法修改代码说如果任何列是“注册的”并且后面的列说“没有行动”但不是“撤回”,让它说注册?在我的真实数据中,第一列已注册,但接下来的列显示无操作。抱歉跟进!
    • @J.Sabree 我认为随着多个条件以及同时对不同列的操作,这变得越来越复杂。也许尝试以长格式重塑数据。但是在您当前的代码中,您可以添加类似which.max(c(...) == "No Action") &gt; which.max(c(...) == "Registered") &amp; which.max(c(...) == "Withdrawn") &lt; which.max(c(...) == "Registered") ~ "Registered" 的条件
    猜你喜欢
    • 1970-01-01
    • 2018-12-09
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-22
    相关资源
    最近更新 更多