首页 > 解决方案 > In R, how can loop multiple variable at the same?

问题描述

I am wondering of how can I loop the multiple variables at the same time in R.

For example,

a = c(1, 2, 3)
b = c(4, 5, 6)

And then for loop, the below code didnt work.

for (i, j in a, b) {
    print(i)
    print(j)
}

In Python,

for i,j in zip(a,b):
  print(i)
  print(j)

it is possible. How can I do this in R?

标签: rloops

解决方案


使用 R 是不可能的,在这种情况下,最好的解决方案是遍历一个数组的长度并在每个数组的位置打印值:

a = c(1, 2, 3)
b = c(4, 5, 6)
for(i in 1:length(a))
{
  print(a[i])
  print(b[i])
}

推荐阅读