Reputation: 229
I am trying to write a double For loop in R, but for the following code the output is not what I need
k<-4
n<-5
for (i in 1:n)
for (j in 1:k+1)
cat(i,j, "\n")
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?
Upvotes: 0
Views: 76
Reputation: 121
The +1 after k is adding to j, try
for (j in 1:(k+1))
to force the addition to happen to k
Upvotes: 2