Return elements of list as independent objects in global environment

前端 未结 3 1995
名媛妹妹
名媛妹妹 2020-11-27 06:31

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:



        
相关标签:
3条回答
  • 2020-11-27 06:37

    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

    0 讨论(0)
  • 2020-11-27 06:55

    This also would work:

    lapply(seq_along(obj), function(i) assign(names(obj)[i], obj[[i]], envir = .GlobalEnv))
    
    0 讨论(0)
  • 2020-11-27 06:59

    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
    
    0 讨论(0)
提交回复
热议问题