r 中的嵌套 foreach 循环 seq() 输出
Nested foreach loop seq() output in r
考虑以下代码:
space<- 2
years<- 4
year<- foreach(1:years, .combine = rbind)%:%
foreach(1:space, .combine = rbind)%do% seq(1:years)
year
[,1] [,2] [,3] [,4]
result.1 1 2 3 4
result.2 1 2 3 4
result.1 1 2 3 4
result.2 1 2 3 4
result.1 1 2 3 4
result.2 1 2 3 4
result.1 1 2 3 4
result.2 1 2 3 4
为什么 return 将输出作为 int[1:8, 1:4]
?我正在寻找的输出应该是 int[1:8, 1]
并且看起来像这样:
[,1]
result.1 1
result.2 1
result.1 2
result.2 2
result.1 3
result.2 3
result.1 4
result.2 4
我想要它 return 这种格式的原因是我正在执行许多其他嵌套 foreach
循环,这些循环依赖于 space
和 [=16] 的迭代器=] 和 space
需要记录为 result.n
.
非常感谢任何帮助。
你 return seq(1:years)
两个(未命名)序列的每个组合 (4x2 = 8)。
要得到你想要的结果,你需要
year <- foreach(year = 1:years, .combine = rbind) %:%
foreach(1:space, .combine = rbind) %do% year
考虑以下代码:
space<- 2
years<- 4
year<- foreach(1:years, .combine = rbind)%:%
foreach(1:space, .combine = rbind)%do% seq(1:years)
year
[,1] [,2] [,3] [,4]
result.1 1 2 3 4
result.2 1 2 3 4
result.1 1 2 3 4
result.2 1 2 3 4
result.1 1 2 3 4
result.2 1 2 3 4
result.1 1 2 3 4
result.2 1 2 3 4
为什么 return 将输出作为 int[1:8, 1:4]
?我正在寻找的输出应该是 int[1:8, 1]
并且看起来像这样:
[,1]
result.1 1
result.2 1
result.1 2
result.2 2
result.1 3
result.2 3
result.1 4
result.2 4
我想要它 return 这种格式的原因是我正在执行许多其他嵌套 foreach
循环,这些循环依赖于 space
和 [=16] 的迭代器=] 和 space
需要记录为 result.n
.
非常感谢任何帮助。
你 return seq(1:years)
两个(未命名)序列的每个组合 (4x2 = 8)。
要得到你想要的结果,你需要
year <- foreach(year = 1:years, .combine = rbind) %:%
foreach(1:space, .combine = rbind) %do% year