0

I have many dataframes (week1, ..., week20) and I want to call them in a loop:

for (i in 1:20) {
  function(weeki)
}

Intuitively, I want to do something like this:

weeki <- paste ("week", i, sep="")

where weeki refers to the dataframe.

I can make a list :

week <- list (week1, week2, ..., week20)
for (i in 1:20) {
  function(week[[i]])
}

But it doesn't help because I have to write the names of the dataframe in that list. I want "week" to be a list of all "weeki" dataframes and that week[[i]] = weeki

  • 1
    [7.21 How can I turn a string into a variable?](http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f) – Joshua Ulrich Jul 20 '13 at 10:37
  • Does `week[[i]] = weeki` refer to the names of the data frames in the list, or to the content of the data frames in the list, or both? – Bryan Hanson Jul 20 '13 at 11:46

1 Answers1

1

Assuming you already have the dataframes week1, week2, etc already, try this:

 for (i in 1:20) {
   week <- get(paste0("week", i))
   [ do something with week ]
 }

Note that you say you want to "call them". You can call a function, not a dataframe, but I assume you mean you want to do something with each of the dataframes.

seancarmody
  • 6,182
  • 2
  • 34
  • 31