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?