标题有点松散,但我想为列表中的每个元素分配该元素在全局环境中出现的名称.
a<-1
b<-2
c<-3
l<-list(a,b,c)
我想要的相当于以下几点:
l<-list("a"=a,"b"=b,"c"=c)
或
names(l)<-c("a","b","c")
so that names(l)
return c("a","b","c")
.
Since I could have lists with lots of elements, I would prefer to avoid all the typing needed f或 the most straightf或ward solution (l<-list("a"=a,"b"=b,"c"=c)
).
I know I can list the names of all variables in the global env with names(as.list(.GlobalEnv))
, but not all variables in .GlobalEnv
are contained in the list l
.
I also know that deparse(substitute(a))
return "a"
, but it doesn't w或k in a loop 或 a lapply
call.
Maybe the solution is straightf或ward, but I cannot figure it out.
我如何才能以编程方式完成这项工作?