I'm confused about purrr::map2. I thought I understood it but I'm stumped here. Can someone help me understand why the first example below is not giving me the output I expected?
library(purrr)
test1 <- list(a = 3, b = 4)
test2 <- list(a = 4:6, b = 5:7)
I want to map along each of these lists and pass the elements stepwise to
a map function
Inside the map function, you wanted to pass each element of .y, which is test2 to the innermost function, where it will be concatenated to .x, the passed element of test1.
My guess that this last part is not working.
.x, the passed element of test1
Inside that innermost function, .x is taking the .x argument of the map function. So, both .x and . refer to basically the same element of .y, resulting in the unexpected answer.
I would like to be wrong here, but I faced these types of errors lot. .x, .y for first, second argument of the function seems to be very helpful, but I get confused when I have to have functions under functions under functions, and don't have control over argument names. That's why I've switched from ~, .x, .y etc. based functions to explicit standard function, like you did.
It's about environments. Each map has it's own environment, so each map has it's own . which is equivalent of .x. The .x is not the object (value) you expect, but you miss the environment aspect.