Compare
mtcars %>% mutate(a = pmap(list(gear, carb), sum))
which "works" as expected, with
mtcars %>% mutate(a = pmap(list(gear, carb), mean))
which does not.
I am obviously missing something.
Compare
mtcars %>% mutate(a = pmap(list(gear, carb), sum))
which "works" as expected, with
mtcars %>% mutate(a = pmap(list(gear, carb), mean))
which does not.
I am obviously missing something.
The difference has to do with the function interface: sum()
accepts any number of arguments (often referred to as "dots"), while mean()
expects a vector:
sum( 1, 2, 3 ) # 6
mean( c(1,2,3) ) # 2
To use a vector-based function like mean()
with pmap()
, you can lift its domain from vector to dots:
mtcars %>% mutate(a = pmap(list(gear, carb), lift_vd(mean))) # Now works as expected