简体   繁体   中英

can we iterate over two lists with purrr (not simultaneously)?

Consider that I have a function f(x,y) that takes two argument and two lists

  • x_var = list('x','y','z')
  • and y_var = list('a','b')

Is there a purrr function that allows me to iterate every combination of one element in x_var and one element in y_var ? That is, doing f(x,a) , f(x,b) , f(y,a) , f(y,b) etc.

The usual solution would be to write a loop, but I wonder if there is more concise here (possibly with purrr .

Thanks!

您可以使用其中一个cross*函数和map

map(cross2(x_var, y_var), unlist)

基本R函数expand.grid函数在这里工作

expand.grid(x=x_var, y=y_var) %>% {paste(.$x, .$y)}

You can nest your calls to map

library(purrr)
map(x_var, function(x) {
  map(y_var, paste, x)
})
[[1]]

[[1]][[1]]
[1] "a x"

[[1]][[2]]
[1] "b x"


[[2]]
[[2]][[1]]
[1] "a y"

[[2]][[2]]
[1] "b y"


[[3]]
[[3]][[1]]
[1] "a z"

[[3]][[2]]
[1] "b z"

If x_var and y_var are atomic vectors or arrays, and the function returns an atomic vector of known length, you can use the base R function outer :

outer(x_var, y_var, FUN = f)

This will return an array with dimensions c(dim(x_var), dim(y_var)) . Dimension names are similarly combined. For example:

x_var <- setNames(1:3, c("a", "b", "c"))
y_var <- setNames(4:5, c("d", "e"))
outer(x_var, y_var, '+')
#   d e
# a 5 6
# b 6 7
# c 7 8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM