0

如何在 R 中的集合的笛卡尔积空间上循环?假设我有一些向量,我想迭代他们的笛卡尔积空间。

is_igg <- c(TRUE,FALSE)
p_val_thres <- c(5e-5,5e-8)
ld_thres <- c(0.5,0.8)

编写三个嵌套在每个级别的 for 循环会很乏味:

for (x in is_igg) {
  for (y in is_igg) {
    for (z in ld_thres) {
      do something with (x,y,z)
    }
  }
}

有没有比这更清洁的替代方案来实现这样的目的?在 Python 中,这很容易,因为可以zip(...)创建一个迭代器。但我不知道如何在 R 中做到这一点。

4

1 回答 1

0

我们可以这样做expand.grid来创建所有组合,然后可以apply通过循环遍历行 ( MARGIN = 1)

apply(expand.grid(is_igg, p_val_thres, ld_thres), 1, FUN = yourfn)
于 2021-10-07T19:25:40.153 回答