Extract dataframes from list using Lapply

Hello,
I have the following list
image

This list has 6 dataframes. Then I need to extract them to my global enviroment using their names, like this:
image

I have been using this code:
image

Is there any elegant lapply solution?

Did you try list2env? Is it not enough for you?

https://stat.ethz.ch/R-manual/R-devel/library/base/html/list2env.html

Here's a mapply solution, which can be wrapped inside invisible:

mapply(function(name, value) assign(name, value, envir=globalenv()), names(datalist), datalist)

(not tested)

1 Like

If the motivation for doing this is to improve the ease of interactive analysis work, i.e. to save ones typing while one plays with the data, there is a possible alternative to consider.
attach() and detach() can be used to add ( and remove) items to the search path, effectively it means you can treat an attached object as if its names components were present in your immediate environment, whens they arent. It makes it as though you typed out the full name effectively.
i.e.


wantlist <- list(a1 = mtcars,
                 a2=iris)

head(a1)
# as expected : Error in head(a1) : object 'a1' not found
head(wantlist$a1)
# gets what I want but annoying to type each time

attach(wantlist)
# more convenient
head(a1)
head(a2)

# restore old behaviour
detach("wantlist")

head(a1)
# as expected : Error in head(a1) : object 'a1' not found

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.