I'm trying to better understand how pmap()
works within dataframes, and I get a surprising result when applying pmap()
to compute means from several columns.
mtcars %>%
mutate(comp_var = pmap_dbl(list(vs, am, cyl), mean)) %>%
select(comp_var, vs, am, cyl)
In the above example, comp_var
is equal to the value of vs in its row, rather than the mean of the three variables in a given row.
I know that I could get accurate results for comp_var
using ...
mtcars %>%
rowwise() %>%
mutate(comp_var = mean(c(vs, am, cyl))) %>%
select(comp_var, vs, am, cyl) %>%
ungroup()
... but I want to understand how pmap()
should be applied in a case like this.
mean(1,2,3)
return? – Pardue