将列表的元素作为全局环境中的独立对象返回 [英] Return elements of list as independent objects in global environment

查看:77
本文介绍了将列表的元素作为全局环境中的独立对象返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,并且想将列表中的元素分解为全局环境中的单独对象.

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

例如,我想要列表:

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

是三个独立的对象abc.

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

但是失败了,使用Error in names(x)[1] <<- x[1] : object 'x' not found.

我如何实现我的目标?

How might I achieve my aim?

推荐答案

有用于将列表映射到环境的特殊功能:

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.这是我的评论作为答案

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

这篇关于将列表的元素作为全局环境中的独立对象返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆