Reputation: 23
I need some help applying a function to four tibbles individually stored in the same list.
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")
}
}
I have a list with 4 tibbles inside of different sizes. I simply want to apply the function above that uses four columns to each tibble, but I'm getting all sorts of errors. I've searched as much as I can and read R4DS and other posts here, but I can't find a solution.
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
The following returns a list with only one value, which I'm not interest in, I want a list with four tibbles with the same dimensions (rows) as the input
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"
The dummy list is the following:
[[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
What am I missing? 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 time to solve my problem.
> 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’
Upvotes: 1
Views: 1322
Reputation: 23
For anyone struggling with mutating columns on several tibbles inside a list object, the below code worked on the problem above:
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)
Upvotes: 1
Reputation: 11255
One issue is that you are passing a single data.frame
to a function that expects 4 arguments. To fix that you could change your function to:
new_fx = function (DF) {
Status = DF$Status
Escalated = DF$Escalated
...
}
map(dummy, new_fx)
The next potential issue is your use of if ... else...
. Because this was not a reproducible example with expected output, I am assuming you want to add a column with the if ... else...
statement. You will want to get rid of the double &&
and ||
because they will evaluate to a single logical value.
Along with that, switch to using ifelse
or, since you are in tidyverse, you could use case_when()
would produce a vector of expected length.
Upvotes: 2