Return elements of list as independent objects in global environment
Asked Answered
L

4

53

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?

Levileviable answered 10/12, 2012 at 5:31 Comment(0)
C
76

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

Cremator answered 10/12, 2012 at 9:17 Comment(0)
N
10

This also would work:

lapply(seq_along(obj), function(i) assign(names(obj)[i], obj[[i]], envir = .GlobalEnv))
Novara answered 10/12, 2012 at 5:45 Comment(6)
You might want obj[[i]] instead, so that you assign vectors, and not one element lists.Kilowatthour
Please not Dason's does what you want as well with less headache.Novara
@ricardo, please take a look at Gregory's comment to your question. -- (the other)Zarathustra
I don't like to attach data, ever. Is there a good reason to break this rule?Levileviable
@Levileviable 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?Paraclete
No shame in unchecking this answer and selecting Gregory's.Novara
W
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"
Wattle answered 22/7, 2021 at 9:53 Comment(0)
P
-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
Paraclete answered 10/12, 2012 at 5:36 Comment(4)
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.Procreate
@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.Paraclete
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.Procreate
@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 tooParaclete

© 2022 - 2024 — McMap. All rights reserved.