purrr pmap按列名编号读取最大列名
我有以下数据集:purrr pmap按列名编号读取最大列名,r,dplyr,conditional-statements,purrr,stringr,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"
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"))
我试过各种方法让R读取状态的数字,但我想不出来。保留代码的其余部分很重要,特别是str_detect()部分,因为虽然我的示例数据是干净的,但真实的数据集有许多状态行和许多看起来像“已完成”和“已完成”的条目
为什么我不能用parse number查看purrr,让它读取最大状态
谢谢大家! 保持一切原样,只处理你的
哪一个.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
库(tidyverse)
问题%>%
变异(
当前=
pmap_chr(选择(,包含(“状态”))~
什么时候(
任何(str_detect(c(…),“(?i)Completed”)~“Completed”,
任何(str_-detect(c(…),“(?i)豁免”));任何(str_-detect(c(…),“(?i)不完整”)~“排除”,
其中.max(c(…)=“已注册”)=“长度(c(…)~”已注册“,
任何(str_-detect(c(…),“(?i)不显示”);任何(str_-detect(c(…),“(?i)拒绝”);任何(str_-detect(c(…),“(?i)取消”);任何(str_-detect(c(…),“(?i)等待名单过期”);(str,
真~“不”
)
)
)
#名称状态\u 1状态\u 2状态\u 3当前
#
#1 Angela注册撤销未采取任何行动
#2 Claire无操作无操作已注册
#3贾斯汀完成注册撤销完成
#4 Bob否认未采取任何行动未采取任何行动
#5 Gil无行动豁免无行动豁免
谢谢!如果您有时间,您能口头解释一下您将如何阅读which.max行,以及为什么它在一个句点内不起作用吗?再次感谢你@J.Sabree在您的尝试中,您将列名与“Registered”
进行比较,而您需要的是将列的值与“Registered”
进行比较,获取值为TRUE
的最大索引,并检查它是否与传递的列总数相同。(length(…)
)。我的真实数据集遇到了一个问题,我的示例中没有这个问题。有没有办法修改代码,说明是否有列“已注册”,后面的列显示“无操作”,但没有“撤回”,让它显示为已注册?在我的真实数据中,第一列是注册的,但是下一列说没有操作。很抱歉跟进@J.Sabree我认为,随着多种条件的出现,以及同时对不同列的操作,这种情况变得越来越复杂。也许可以改为尝试以长格式重塑数据。但是在您当前的代码中,您可以添加一个条件,例如which.max(c(…)=“无操作”)>which.max(c(…)=“已注册”)&which.max(c(…)=“已撤销”)
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"
)
)
)
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