Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a multiple data frames in FOR-LOOP [duplicate]

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?

like image 269
user1783504 Avatar asked Jul 14 '26 21:07

user1783504


1 Answers

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.

like image 108
Sven Hohenstein Avatar answered Jul 16 '26 09:07

Sven Hohenstein