purrr pmap 按列名编号读取最大列名
purrr pmap to read max column name by column name number
我有这个数据集:
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"))
我想做一个专栏,里面有每个人现在的状态。如果此人曾经完成课程,则他们已完成。如果他们曾被豁免,他们将被排除在外。如果它们不是已注册(或已完成或已豁免),则它们是 "Not Taken." 困难的是我希望我的代码说它们仅在它们的最后一个操作被注册时才被注册。所以,它应该是这样的:
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 以使其读取最大状态?
谢谢!
保持一切原样,只处理您的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
我有这个数据集:
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"))
我想做一个专栏,里面有每个人现在的状态。如果此人曾经完成课程,则他们已完成。如果他们曾被豁免,他们将被排除在外。如果它们不是已注册(或已完成或已豁免),则它们是 "Not Taken." 困难的是我希望我的代码说它们仅在它们的最后一个操作被注册时才被注册。所以,它应该是这样的:
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 以使其读取最大状态?
谢谢!
保持一切原样,只处理您的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