3

I am trying to use pmap() from the purrr package to simplify my code.

I have a list of vectors x; all the vectors are the same length. I want to find the mean and variance of all nth elements across all vectors in the list. That is, I want the mean and variance of all the first elements, all the second elements, and so on.

Before the tidyverse, I would convert x to a matrix and use apply().

x <- list(1:10, 1:10, 1:10)
x_matrix <- do.call(cbind, x)
apply(x_matrix, 1, mean)
##  [1]  1  2  3  4  5  6  7  8  9 10
apply(x_matrix, 1, var)
##  [1] 0 0 0 0 0 0 0 0 0 0

pmap() should allow this without the matrix conversion. pmap_dbl() can replace the apply() and mean() calculation above.

library(purrr)
pmap_dbl(x, mean)
##  [1]  1  2  3  4  5  6  7  8  9 10

However, I cannot get pmap_dbl() and var() and calculation to work. I get NA for every variance.

pmap_dbl(x, var)
##  [1] NA NA NA NA NA NA NA NA NA NA

What am I missing?

Richard Herron
  • 9,760
  • 12
  • 69
  • 116

1 Answers1

4

We can use ~ and then with ... get the elements, and apply the function

pmap_dbl(x, ~ var(c(...)))

The reason for different behavior is the difference in the number of parameters in mean and var. In mean, after the object x, other parameters go into ..., while in var, it is not the case, there is x, there is y etc.

akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Hah! Answered faster than I can refresh (and faster than SO allows me to accept an answer). I knew about the `~`. I should have known about the `...`. – Richard Herron May 18 '18 at 18:01