55

I have a list, and would like to break the elements of the list into seperate objects in the global environment.

For example, I would like the list:

obj <- list(a=1:5, b=2:10, c=-5:5) 

to be three seperate objects a, b, and c.

I tried to achieve this with:

lapply(obj, FUN = function(x) names(x)[1] <<- x[1]) 

But it failed, with Error in names(x)[1] <<- x[1] : object 'x' not found.

How might I achieve my aim?

0

4 Answers 4

78

There is special function for mapping list to environment:

> obj <- list(a=1:5, b=2:10, c=-5:5) > ls() [1] "obj" > list2env(obj,globalenv()) <environment: R_GlobalEnv> > ls() [1] "a" "b" "c" "obj" 

P. S. It is my comment provided as an answer

Sign up to request clarification or add additional context in comments.

Comments

10

This also would work:

lapply(seq_along(obj), function(i) assign(names(obj)[i], obj[[i]], envir = .GlobalEnv)) 

6 Comments

You might want obj[[i]] instead, so that you assign vectors, and not one element lists.
Please not Dason's does what you want as well with less headache.
@ricardo, please take a look at Gregory's comment to your question. -- (the other)
I don't like to attach data, ever. Is there a good reason to break this rule?
@ricardo Gregory's answer isn't using attach (which is a bad idea and why I don't recommend it). Then again if you don't like attaching data why are you doing what you want to do in the question instead of working directly with the list itself?
|
1

In case the list isn't yet a named list, we need to set names first, e.g. with incrementing letters.

obj2 <- list(1:5, 2:10, -5:5) list2env(setNames(obj2, letters[seq(obj2)]), envir=.GlobalEnv) ls() # [1] "a" "b" "c" "obj2" 

Comments

-1

I don't recommend it but you could use attach

> obj <- list(a=1:5, b=2:10, c=-5:5) > attach(obj) > a [1] 1 2 3 4 5 > b [1] 2 3 4 5 6 7 8 9 10 > c [1] -5 -4 -3 -2 -1 0 1 2 3 4 5 

4 Comments

This does not what OP asked, it just attaches the object obj to the search path. That doesn't mean you assign the elements of the list to independent objects in the global environment. Gregory has the correct answer.
@JorisMeys Sure but they never really said why they wanted to do this either. attach allows you to pretend like they're a part of the global environment (at least in simple cases) with very little work. With that said I definitely think Gregory's answer is the best out of the answers given.
I see why you mentioned it, but the use of attach poses a lot more problems than it solves, not in the least when you try to change one of the elements in the list. As said in the R Style Guide : The possibilities for creating errors when using attach are numerous. Avoid it.
@JorisMeys I agree. Which is why I don't recommend it. I also don't really like the whole idea posted in the question though. There are also dangers associated with Gregory's answer as well too

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.