55

This is possibly a really simple question. I have a list of dataframes (df1, df2.... dfn), i.e. each element of the list is a dataframe. So basically, the list was created like this:

mylist = list(df1, df2,...., dfn)

But how do I do the reverse, that is unlist so that df1, df2, etc. reside separately in the workspace?

Jaap
  • 81,064
  • 34
  • 182
  • 193
user702432
  • 11,898
  • 21
  • 55
  • 70
  • 2
    Thanks. Are you a telepath? – user702432 Jul 17 '13 at 10:31
  • 3
    The question *should* be, why would you *want* to do this. If you have all your df's in a lovely list, there's hardly ever a reason you need to make them into separate objects. You can just work with them as elements of your list. – Simon O'Hanlon Jul 17 '13 at 10:34
  • 2
    Agree with Simon. If you want to call them by name its better to name in them in the list - e.g. `myList = list(df1=data.frame(...), df2=data.frame(...), ...)` and call them with `myList[['df1']` – geotheory Jul 17 '13 at 10:44
  • 1
    Just a matter of convenience, I guess. With RStudio, it's easier to keep track of all the dataframes. – user702432 Jul 17 '13 at 16:43

2 Answers2

93

Use list2env it is specially designed for this:

From a named list x, create an environment containing all list components as objects, or “multi-assign” from x into a pre-existing environment.

So here :

list2env(mylist ,.GlobalEnv)
agstudy
  • 119,832
  • 17
  • 199
  • 261
20

You could simply use a for-loop along with the assign function like that:

# Sample data
df.list <- list(data.frame(x = 1:3, y = c(10, 20, 30)), 
                data.frame(x = 4:6, y = c(40, 50, 60)), 
                data.frame(x = 7:9, y = c(70, 80, 90)))

# Write out single data frames
for (i in seq(df.list))
  assign(paste0("df", i), df.list[[i]])
fdetsch
  • 5,239
  • 3
  • 30
  • 58