r - R - Purrr - 将功能应用于列表中的小标题
问题描述
我需要一些帮助,将函数应用于分别存储在同一列表中的四个小标题。
功能:
status_fun <- function(Status,
Escalated,
Created,
Resolved
){
if(Escalated == "Yes"){
return("Escalated")
} else if(Status == "Closed" && (month(Created) == month(Resolved) || Resolved - Created < 5
)
){
return("Closed")
} else {
return("Not Solved")
}
}
我有一个列表,里面有 4 个不同大小的小方块。我只是想将上面使用四列的函数应用于每个小标题,但我遇到了各种各样的错误。我已经尽可能多地搜索并阅读了 R4DS 和其他帖子,但我找不到解决方案。
dummy %>%
map(., status_fun)
Error in .f(.x[[i]], ...) :
argument "Escalated" is missing with no default
dummy %>%
map(~ map(., status_fun))
Error in .f(.x[[i]], ...) :
argument "Escalated" is missing with no default
以下返回一个只有一个值的列表,我对此不感兴趣,我想要一个具有与输入相同维度(行)的四个小标题的列表
dummy %>%
map(., ~ status_fun(Status = 'Status', Escalated = 'Escalated', Created = 'Created', Resolved = 'Resolved'))
[[1]]
[1] "Not Solved"
[[2]]
[1] "Not Solved"
[[3]]
[1] "Not Solved"
[[4]]
[1] "Not Solved"
虚拟列表如下:
[[1]]
# A tibble: 589 x 5
Created Resolved Status Country Escalated
<date> <date> <chr> <chr> <chr>
1 2020-04-03 2020-04-08 Closed Luxembourg No
2 2020-03-31 NA In Progress France No
3 2020-03-31 NA In Progress France No
4 2020-03-31 NA In Progress Luxembourg No
5 2020-03-31 NA In Progress Luxembourg No
6 2020-03-30 NA In Progress France Yes
7 2020-03-27 NA In Progress Ireland No
8 2020-03-27 2020-04-10 Closed Luxembourg No
9 2020-03-27 NA In Progress Luxembourg No
10 2020-03-27 2020-03-30 Closed Ireland No
# ... with 579 more rows
[[2]]
# A tibble: 316 x 5
Created Resolved Status Country Escalated
<date> <date> <chr> <chr> <chr>
1 2020-04-13 NA Open Luxembourg No
2 2020-04-13 NA Open Spain No
3 2020-04-07 NA Open France No
4 2020-04-03 NA In Progress Luxembourg No
5 2020-03-30 NA Awaiting Information Luxembourg No
6 2020-03-30 NA Awaiting Information France Yes
7 2020-03-30 2020-03-31 Closed France No
8 2020-03-30 NA Awaiting Information France No
9 2020-03-30 NA Awaiting Information Spain No
10 2020-03-30 NA Awaiting Information Sweden No
# ... with 306 more rows
[[3]]
# A tibble: 64 x 5
Created Resolved Status Country Escalated
<date> <date> <chr> <chr> <chr>
1 2020-04-13 NA Open Chile No
2 2020-04-10 NA Open Mexico Yes
3 2020-04-10 NA Awaiting Information Mexico No
4 2020-04-09 NA Open Chile No
5 2020-04-03 2020-04-06 Closed Mexico Yes
6 2020-04-02 2020-04-02 Closed Mexico No
7 2020-04-01 2020-04-01 Closed Mexico No
8 2020-03-31 2020-04-01 Closed Brazil No
9 2020-03-30 2020-03-31 Closed Mexico No
10 2020-03-27 2020-04-06 Closed Mexico No
# ... with 54 more rows
[[4]]
# A tibble: 30 x 5
Created Resolved Status Country Escalated
<date> <date> <chr> <chr> <chr>
1 2020-04-13 NA Open Chile No
2 2020-04-07 NA Open Brazil No
3 2020-03-23 2020-03-25 Closed Chile No
4 2020-03-17 2020-03-18 Closed Chile No
5 2020-03-16 NA Open Mexico No
6 2020-03-11 2020-03-11 Closed Brazil No
7 2020-03-11 2020-03-12 Closed Brazil No
8 2020-03-10 2020-03-10 Closed Brazil No
9 2020-03-09 NA In Progress Brazil No
10 2020-03-02 2020-03-03 Closed Brazil No
# ... with 20 more rows
我错过了什么? 我已经尝试了各种 pmap,map_2,此处的说明Code not working using map from purrr package in R 和此处Apply function to nested loop (purrr package?) 但没有成功..提前感谢愿意接受他们的人是时候解决我的问题了。
> version _
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 4
minor 0.0
year 2020
month 04
day 24
svn rev 78286
language R
version.string R version 4.0.0 (2020-04-24)
nickname Arbor Day
packageVersion("tidyverse")
[1] ‘1.3.0’
packageVersion("lubridate")
[1] ‘1.7.8’
解决方案
一个问题是您将单个传递给data.frame
需要 4 个参数的函数。要解决此问题,您可以将功能更改为:
new_fx = function (DF) {
Status = DF$Status
Escalated = DF$Escalated
...
}
map(dummy, new_fx)
下一个潜在问题是您使用if ... else...
. 因为这不是具有预期输出的可重现示例,所以我假设您想在if ... else...
语句中添加一列。您将希望摆脱双重&&
,||
因为它们将评估为单个逻辑值。
除此之外,切换到 usingifelse
或者,因为您在tidyverse中,您可以使用case_when()
将产生预期长度的向量。
推荐阅读
- regex - 用下划线分隔特定单词,但不是复数形式
- javascript - 使用三元操作的 JSON 遍历
- python - Django Rest Framework 自定义权限验证
- javascript - 我如何编写代码 Javascript 使一些代码弹出窗口在某些国家/地区工作
- java - Dijkstra 算法自身的实现问题
- python - 在 Pandas DataFrame 中查找连续日期组
- apache - MOD_WRITE Apache Tomcat REWRITE CONDITIONS 结合 request_uri 和 request_method
- javascript - 创建 SVG 使用
- javascript - 如果我手动更改选中状态,为什么我的 if 语句有效,但当我单击按钮时无效
- c++ - OpenGL转换不使用GLM矩阵