Use pmap in purrr package of R to print every value of 3 vectors of varying length

43 views Asked by At

I am trying to print out the vectors using pmap [might not be the best method, is a homework exercise to try out using pmap on different vector lengths, so need to use pmap function]

set.seed(0)

# Load and use MCMCglmm and purrr packages in R
library(MCMCglmm)
library(purrr)

# Create three vectors of varying lengths with lower and upper bounds using rtnorm function from MCMCglmm
vector1 <- rtnorm(n = 3, lower = 1, upper = 3)
vector2 <- rtnorm(n = 10, lower = 4, upper = 20)
vector3 <- rtnorm(n = 7, lower = 6, upper = 9)

# Create a list with all vectors
my_args <- as.list(vector1, vector2, vector3)

# Generate three vectors using pmap (pmap(my_args, my_function)]
pmap(my_args, print)`

The output is not what I was expecting though, I wanted it to simply print every value for every item in the list "my_args". Current output is shown below

[1] 1
[[1]]
[1] 1.341292

I wanted to use pmap to simply print every value for every item in the list "my_args", expecting it to return every number in vector1, vector 2, and vector 3. The vectors are different lengths.

0

There are 0 answers