[英]R - Purrr - Apply Function to Tibbles in List
我需要一些幫助,將 function 應用於分別存儲在同一列表中的四個小標題。
Function:
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 個不同大小的小方塊。 我只是想應用上面的 function ,每個小標題使用四列,但我遇到了各種各樣的錯誤。 我已經盡可能多地搜索並閱讀了 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
我錯過了什么? I've tried all sorts of pmap, map_2, the instructions here Code not working using map from purrr package in R and here Apply function to nested loop (purrr package?) with no success.. Thanks in advance for someone willing to take their是時候解決我的問題了。
> 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
傳遞給 function,它需要 4 個 arguments。 要解決此問題,您可以將 function 更改為:
new_fx = function (DF) {
Status = DF$Status
Escalated = DF$Escalated
...
}
map(dummy, new_fx)
下一個潛在問題是您對if... else...
的使用。 因為這不是預期 output 的可重現示例,所以我假設您要添加帶有if... else...
語句的列。 你會想要擺脫雙重&&
和||
因為它們將評估為單個邏輯值。
除此之外,切換到使用ifelse
或者,因為你在tidyverse ,你可以使用case_when()
會產生一個預期長度的向量。
對於在 object 列表中的幾個小標題上苦苦掙扎的人來說,下面的代碼可以解決上述問題:
status_fun <- function(df){
Escalated = df$Escalated
Status = df$Status
Created = df$Created
Resolved = df$Resolved
dplyr::mutate(df,
Status = case_when(
Escalated == "Yes" ~ "Escalated",
(Status == "Closed" &
(month(Created) == month(Resolved) | Resolved - Created < 5)) ~ "Closed",
TRUE ~ "Not Solved"
)
)
}
dummy <- dummy %>% map(., status_fun)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.