Let's say that I have many objects in my workspace (global environment) and I want to store most of those in a list. Here's a simplified example:
假设我的工作空间(全局环境)中有很多对象,我希望将大多数对象存储在列表中。这是一个简化的例子:
# Put some objects in the workspace
A <- 1
B <- 2
C <- 3
I would like to store objects A and C in a list. Of course, I can do that explicitly:
我想将对象A和C存储在列表中。当然,我可以明确地做到这一点:
mylist <- list(A,C)
However, when the number of objects in the workspace is very large, this would become rather cumbersome. Hence, I would like to do this differently and attempted the following:
但是,当工作空间中的对象数量非常大时,这将变得相当麻烦。因此,我想以不同的方式做到这一点并尝试以下方法:
mylist <- list(setdiff(ls(),B))
But this obviously is not what I want, as it only stores the names of the objects in the workspace.
但这显然不是我想要的,因为它只存储工作区中对象的名称。
Any suggestions on how I can do this?
有关如何做到这一点的任何建议?
Many thanks!
3 个解决方案
#1
10
Another option is to use mget
:
另一种选择是使用mget:
mget(setdiff(ls(),"B"))
#2
2
EDIT : I think using lapply
/ sapply
here raises too many problems. You should definitely use the mget
answer.
编辑:我认为在这里使用lapply / sapply会引发太多问题。你绝对应该使用mget答案。
You can try :
你可以试试 :
mylist <- sapply(setdiff(ls(),"B"), get)
In certain cases, ie if all the objects in your workspace are of the same type, sapply
will return a vector. For example :
在某些情况下,即如果工作空间中的所有对象属于同一类型,则sapply将返回向量。例如 :
sapply(setdiff(ls(),"B"), get)
# A C
# 1 3
Otherwise, it will return a list :
否则,它将返回一个列表:
v <- list(1:2)
sapply(setdiff(ls(),"B"), get)
# $A
# [1] 1
#
# $C
# [1] 3
#
# $v
# [1] 1 2
So using lapply
instead of sapply
here could be safer, as Josh O'Brien pointed out.
因此,正如Josh O'Brien指出的那样,在这里使用lapply而不是sapply可能更安全。
#3
2
mget
is definitely the easiest to use in this situation. However, you can achieve the same with as.list.environment
and eapply
:
在这种情况下,mget绝对是最容易使用的。但是,您可以使用as.list.environment和eapply实现相同的功能:
e2l <- as.list(.GlobalEnv)
# or: e2l <- as.list(environment())
# using environment() within a function returns the function's env rather than .GlobalEnv
e2l[! names(e2l) %in "B"]
# the following one sounds particularly manly with `force`
e2l <- eapply(environment(), force)
e2l[! names(e2l) %in "B"]
And one-liners:
(function(x) x[!names(x)%in%"B"])(eapply(environment(), force))
(function(x) x[!names(x)%in%"B"])(as.list(environment()))
#1
10
Another option is to use mget
:
另一种选择是使用mget:
mget(setdiff(ls(),"B"))
#2
2
EDIT : I think using lapply
/ sapply
here raises too many problems. You should definitely use the mget
answer.
编辑:我认为在这里使用lapply / sapply会引发太多问题。你绝对应该使用mget答案。
You can try :
你可以试试 :
mylist <- sapply(setdiff(ls(),"B"), get)
In certain cases, ie if all the objects in your workspace are of the same type, sapply
will return a vector. For example :
在某些情况下,即如果工作空间中的所有对象属于同一类型,则sapply将返回向量。例如 :
sapply(setdiff(ls(),"B"), get)
# A C
# 1 3
Otherwise, it will return a list :
否则,它将返回一个列表:
v <- list(1:2)
sapply(setdiff(ls(),"B"), get)
# $A
# [1] 1
#
# $C
# [1] 3
#
# $v
# [1] 1 2
So using lapply
instead of sapply
here could be safer, as Josh O'Brien pointed out.
因此,正如Josh O'Brien指出的那样,在这里使用lapply而不是sapply可能更安全。
#3
2
mget
is definitely the easiest to use in this situation. However, you can achieve the same with as.list.environment
and eapply
:
在这种情况下,mget绝对是最容易使用的。但是,您可以使用as.list.environment和eapply实现相同的功能:
e2l <- as.list(.GlobalEnv)
# or: e2l <- as.list(environment())
# using environment() within a function returns the function's env rather than .GlobalEnv
e2l[! names(e2l) %in "B"]
# the following one sounds particularly manly with `force`
e2l <- eapply(environment(), force)
e2l[! names(e2l) %in "B"]
And one-liners:
(function(x) x[!names(x)%in%"B"])(eapply(environment(), force))
(function(x) x[!names(x)%in%"B"])(as.list(environment()))