Unlist a list of dataframes

R

R Problem Overview


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?

R Solutions


Solution 1 - R

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)

Solution 2 - R

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]])

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser702432View Question on Stackoverflow
Solution 1 - RagstudyView Answer on Stackoverflow
Solution 2 - RfdetschView Answer on Stackoverflow