3

I would like to create data frames from a FOR-LOOP in R.

Basically, I would like to do something like this:

for (i in 1:3) { x"i"= 1+i}

In this case, I would like to get 3 dataframes:

  • x1 that would only contain 2
  • x2 that would only contain 3
  • x3 that would only contain 4

Is there a way to do this in R?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1783504
  • 331
  • 4
  • 7
  • 14
  • (1) Why do you want data frames to store a single number? (2) Is it possible to use a list instead of multiple data frames? – Sven Hohenstein Oct 06 '13 at 13:54
  • This is just to simplify the question. Basically, I need different subsets of a dataframe and I would like to store them in x"i" data frames for future use. – user1783504 Oct 06 '13 at 13:58

1 Answers1

9
for (i in 1:3) {
  assign(paste0("x", i), i + 1)
}

This will create objects x1, x2, and x3 with the values of i + 1, i.e., 2-4.

Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168