[英]Double (nested) FOR loop in R
I am trying to write a double For loop in R, but for the following code the output is not what I need我正在尝试在 R 中编写一个双 For 循环,但是对于以下代码,output 不是我需要的
k<-4
n<-5
for (i in 1:n)
for (j in 1:k+1)
cat(i,j, "\n")
Output: Output:
> for (i in 1:n)
+ for (j in 1:k+1)
+ cat(i,j, "\n")
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 2
3 3
3 4
3 5
4 2
4 3
4 4
4 5
5 2
5 3
5 4
5 5
Why does j start with 2?为什么j以 2 开头?
The +1 after k is adding to j, try k后的+1加到j上,试试
for (j in 1:(k+1))
to force the addition to happen to k强制加法发生在 k
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.