Sorry to jump in a long-dead thread, but this is clearly an important topic, as several SO questions addressing are highly "liked" e.g, in this SO question and also because hadley seems to be "questioning" the best approach in this GH issue
I've read through several threads and it seems it's prefered not to use rowwise
, but pmap
instead.
As solely an end-user, this approach is not nearly as intuitive as the rowwise
approach (to me at least) for a few reasons.
- The
pmap
help and examples are not very informative (lumped into purrr::map et al) (as pointed out below as well). -
pmap_...
doesn't quite work the same as othermap_
syntax (my attempts below) - I don't understand why there can't be a
mutate_rowwise
-type option, so the underlying data grouping is not altered?
I'm sure much of this is just my limited understanding of the internals, but I've managed to maneuver the purrr
framework much easier than pmap
for some reason.
library(tidyverse)
mtcars %>% as_tibble() %>%
mutate(new_mean_var = mean(c(vs, am, gear, carb)),
new_mean_pmap = pmap_dbl(.l = list(vs, am, gear, carb), mean), # NO
new_mean_pmap_attempt2 = pmap_dbl(.l = list(vs, am, gear, carb), ~mean(c(vs, am, gear, carb))), # NO
new_mean_pmap_attempt3 = pmap_dbl(.l = list(vs, am, gear, carb), function(x,y,z, zz) mean(c(x,y,z, zz)))) # YES