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"))
我要创建一列拥有每个人的当前状态。如果此人曾经完成课程,那么他们就完成了。如果曾经豁免,则将其排除在外。如果它们不是已注册(或已完成或被豁免)的其他内容,则它们是未采用。很难的是,我要我的代码说只有在他们的最后一个动作被注册的情况下,它们才被注册。因此,它看起来应该像这样:
I want to make a column that has everyone's current status. If the person has ever completed the course, they are completed. If they were ever exempt, they are excluded. If they are anything else other than registered (or completed or exempt), they are "Not Taken." What's hard is that I want my code to say they were registered ONLY if their last action was being registered. So, it should look like this:
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()行:
I have this code, and the part that won't work is the which.max() line:
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()部分,因为尽管示例数据是干净的,但实际数据集具有许多状态行和许多看起来像已完成和已完成的条目。
I've tried every way for R to read the numbers of status, but I can't figure it out. It's important that I keep the rest of the code, especially the str_detect() portion because, while my sample data is clean, the real dataset has many rows of status and many entries that look like "COMPLETED" and "completed."
为什么我不看带有解析号的purrr才能读取最大状态?
Why can I not look at purrr with parse number to have it read the max status?
谢谢! / p>
Thank you!
推荐答案
按原样保留所有内容,并仅处理您的 which.max
问题,我们可以做到
Keeping everything as it is and dealing only with your which.max
issue, we can do
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
这篇关于purrr pmap通过列名编号读取最大列名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!