I have a function like the following one, which generates various variables with similar names. While inside this function, I want to get all of the variables into a list. I know this example doesn't seem very efficient, as I could use lists from the first place, but in my function it is necessary I do this.
我有一个像下面这样的函数,它生成具有相似名称的各种变量。在这个函数中,我想把所有的变量都放到一个列表中。我知道这个例子似乎不是很有效,因为我可以从一开始就使用列表,但是在我的函数中,我必须这样做。
test_function <- function(x) {
myenv <- new.env()
myenv$hello1 = "hello1string"
myenv$hello2 = "hello2string"
myenv$cello2 = "hello2string"
mylist <- lapply(ls(name = myenv, pattern = "hello"), get)
print(class(mylist))
}
How do I get all of the variables starting with "hello" into a list without it giving the error: Error in FUN(X[[i]], ...) : object 'hello1' not found when running test_function()
. This even happens when putting the variables into a shared environment.
如何将以“hello”开头的所有变量放入一个列表中,而不产生错误:在FUN(X[I]],…):在运行test_function()时没有找到对象‘hello1’。甚至在将变量放入共享环境时也会发生这种情况。
I would like the final mylist
to be of class list, not character.
我希望最终的mylist是类列表,而不是字符。
Thanks!
谢谢!
1 个解决方案
#1
2
You can create an environment and then create variables inside it. Then using ls()
function with the environment name and the correct pattern, you can see the list of variables in the environment that matches the given pattern.
您可以创建一个环境,然后在其中创建变量。然后使用带有环境名和正确模式的ls()函数,您可以看到与给定模式匹配的环境中的变量列表。
test_function <- function(x) {
myenv <- new.env()
myenv$hello1 = "hello1"
myenv$hello2 = "hello2"
myenv$cello2 = "hello2"
mylist <- ls(name = myenv, pattern = "hello")
print(mylist)
}
test_function(1)
# [1] "hello1" "hello2"
You can use mget
to extract values for a list of variables inside an environment.
可以使用mget提取环境中的变量列表的值。
test_function <- function(x, y, z, pattern) {
myenv <- new.env()
ls_vars <- list( hello1 = x,
hello2 = y,
cello2 = z)
list2env( ls_vars, myenv ) # add list of variables to myenv environment
newvar <- "hello3"
assign(newvar, value = "dfsfsf", envir = myenv) # assign new variable
mylist <- ls(name = myenv, pattern = pattern)
return(mget(mylist, envir = myenv))
}
test_function(x = "hello1", y = "hello2", z = "sdfsd", pattern = "hello")
# $hello1
# [1] "hello1"
#
# $hello2
# [1] "hello2"
#
# $hello3
# [1] "dfsfsf"
test_function(x = "hello1", y = "hello2", z = "sdfsd", pattern = "cello")
# $cello2
# [1] "sdfsd"
#1
2
You can create an environment and then create variables inside it. Then using ls()
function with the environment name and the correct pattern, you can see the list of variables in the environment that matches the given pattern.
您可以创建一个环境,然后在其中创建变量。然后使用带有环境名和正确模式的ls()函数,您可以看到与给定模式匹配的环境中的变量列表。
test_function <- function(x) {
myenv <- new.env()
myenv$hello1 = "hello1"
myenv$hello2 = "hello2"
myenv$cello2 = "hello2"
mylist <- ls(name = myenv, pattern = "hello")
print(mylist)
}
test_function(1)
# [1] "hello1" "hello2"
You can use mget
to extract values for a list of variables inside an environment.
可以使用mget提取环境中的变量列表的值。
test_function <- function(x, y, z, pattern) {
myenv <- new.env()
ls_vars <- list( hello1 = x,
hello2 = y,
cello2 = z)
list2env( ls_vars, myenv ) # add list of variables to myenv environment
newvar <- "hello3"
assign(newvar, value = "dfsfsf", envir = myenv) # assign new variable
mylist <- ls(name = myenv, pattern = pattern)
return(mget(mylist, envir = myenv))
}
test_function(x = "hello1", y = "hello2", z = "sdfsd", pattern = "hello")
# $hello1
# [1] "hello1"
#
# $hello2
# [1] "hello2"
#
# $hello3
# [1] "dfsfsf"
test_function(x = "hello1", y = "hello2", z = "sdfsd", pattern = "cello")
# $cello2
# [1] "sdfsd"