0

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.

Mark
  • 7,785
  • 2
  • 14
  • 34
  • Hi Christine! Welcome to StackOverflow! I added a set.seed(0) to the start of your code, to make it reproducible – Mark Aug 13 '23 at 12:57
  • there's a couple of issues with your code: 1. `as.list(vector1, vector2, vector3)` isn't what you think it is (try just taking a look at my_args normally, you'll see it's just vector1 as a list). – Mark Aug 13 '23 at 13:03
  • 2., pmap isn't the greatest function to be using in this situation, because pmap iterates through many inputs in parallel. So, in this case, because one vector has 3 items, and another has 10, it fails, because when it gets to the fourth element, there's no more items in the one with – Mark Aug 13 '23 at 13:10
  • I believed in some circumstances values can be recycled, because pmap was modelled on pmin and pmax, but I'm not so sure now e.g. pmap(list(1:10, 5:9), max) doesn't work, but pmax(1:10, 5:9) does – Mark Aug 13 '23 at 13:25
  • Does this answer your question? [pmap over lists of differing length](https://stackoverflow.com/questions/52795438/pmap-over-lists-of-differing-length) – Mark Aug 13 '23 at 13:29
  • 1
    Additionally, adding print statements in `map` might not have the outcome you are expecting. See for example `map(1:3,print)`. You end up printing the values to the console as well as returning the list of values. – AndS. Aug 13 '23 at 17:29

0 Answers0