0

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.

Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74
JRC
  • 239
  • 1
  • 7

1 Answers1

0

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
Artem Sokolov
  • 13,196
  • 4
  • 43
  • 74