R, adding data frames in a loop

Viewed 50

In R, how to combine data frames in for loop.

I have data frames item1, item2, item3... and I want to combine them into pairs of item11, item12, item13, item21, item 22, ..., so below is the for loop:

I am having trouble finding the right code to access item1, item2, item3... individually. In Python, it can be done using "item{}.format(i), item{}.format(j)", but is there a similar expression in R? Or it needs to be done otherwisely? Thanks in advance!!

for (j in 1:3){ 
  for (i in 1:3){ 
    assign(paste0("item", i, j), rbind(item_i, item_j))
  }
}
1 Answers

Using your loops this should work:

for (j in 1:3){ 
  for (i in 1:3){ 
    assign(paste0("item", i, j), rbind(get(paste0("item_", i)), get(paste0("item_", j))))
  }
}
Related