Reputation: 7545
I want to rbind a loop that generates data.frames. Here's a [incorrect] loop of the general idea I'm looking for.
for (i in 1:2){
a <- c(2, (i+10))
b <- c((i+10)), 5))
c[i] <- data.frame(a,b)
}
rbind(c)
I want an output like this:
2 11
11 5
2 12
12 5
This question has been asked before, but the answer was a direct solution, with no explanation. I don't know how to read it. It involved do.call
and mget
.
Upvotes: 2
Views: 24862
Reputation: 1678
You don't have to use do.call
for this. Just modify the loop as shown below:
out=NULL
for (i in 1:2){
a <- c(2, (i+10))
b <- c((i+10), 5)
c <- data.frame(a,b)
out=rbind(out,c)
}
out
# a b
# 2 11
# 11 5
# 2 12
# 12 5
Note: You had a bunch of extra parentheses for the b object. That I also took out.
Upvotes: 10