I have a problem with passing arguments to purrr::pmap
when using with mutate
.
I don't understand why some things work and some don't.
My example data:
sdf <- tibble(
col_id = c("id1", "id2", "id3", "id4", "id5", "id6", "id7", "id8", "id9", "id10"),
col_a = c(0.7, 0.3, 1.4, 0.7, 0.5, 1.1, 0.1, 0.6, 1.7, 0.5),
col_b = c(NA, 0.6, 0.2, 0.2, 0.7, 0.2, 0.7, 3.7, 0.7, 0.7),
col_c = c(0.3, 0.4, 1.0, NA, 3.1, 0.2, 0.4, 1.0, 0.1, 0.5))
params = c("col_a", "col_b", "col_c")
Then I want to execute some functions in rows using pmap_dbl
.
First code (below) evaluates as intended.
# code 1
sdf_2 <- sdf %>%
select(all_of(params)) %>%
mutate(sum_p = pmap_dbl(., sum, na.rm = TRUE))
But the same syntax doesn't work with a different function:
sdf_2 <- sdf %>%
select(all_of(params)) %>%
mutate(mean_p = pmap_dbl(., mean, na.rm = TRUE))
Error in mutate(., mean_p = pmap_dbl(., mean, na.rm = TRUE)) : Caused by error in
mean.default()
: ! argument "x" is missing, with no default
Also, when I try to pass parameters to sum function directly - not by ... it does not work
sdf_2 <- sdf %>%
select(all_of(params)) %>%
mutate(sum_p = pmap_dbl(., sum(na.rm = TRUE)))
Error in mutate(., sum_p = pmap_dbl(., sum(na.rm = TRUE))) : Caused by error in
pluck()
: ! argument "x" is missing, with no default
What is the correct way to pass parameters to functions inside pmap when working on whole dataframe horizontally?
Next question:
Is there any way to pas column names stored in params to perform function in pmap only on them?
select(all_of(params))
works but result dataframe has no id column. It's easy to recreate, but would be nice to not remove it at all.
Why can't I parse
mean
topmap
?Try:
mean
take argumentx
,sum
takes...
directly (check documentation). You'll need:I.e.
Output:
How to to specify variables in
pmap
?cur_data()
across
Output:
The fast way: Using rowMeans and rowSums with
across
:Update: Add fourth way