I am trying to grow a list in R, where both the value and name of each entry is held in a variable, but it doesn't seem to work.
我试图在R中增加一个列表,其中每个条目的值和名称都保存在变量中,但它似乎不起作用。
my_models_names <- names(my_models)
my_rocs=list()
for (modl in my_models_names) {
my_probs <- testPred[[modl]]$Y1
my_roc <- roc(Ytst, my_probs)
c(my_rocs, modl=my_roc) # <-- modl and my_roc are both variables
}
My list my_rocs
is empty at the end, even though I know that the loop iterates (my_roc
is filled in) Why?
我的列表my_rocs最后是空的,即使我知道循环迭代(my_roc已填入)为什么?
On a related note, is there a way to do this without looping?
在相关的说明中,有没有办法在没有循环的情况下执行此操作?
3 个解决方案
#1
13
Generally in R, growing objects is bad. It increases the amount of memory used over starting with the full object and filling it in. It seems you know what the size of the list should be in advance.
通常在R中,生长对象很糟糕。它会增加从完整对象开始并填充它所使用的内存量。看来你应该提前知道列表的大小。
For example:
my_keys <- letters[1:3]
mylist <- vector(mode="list", length=length(my_keys))
names(mylist) <- my_keys
mylist
## $a
## NULL
## $b
## NULL
## $c
## NULL
You can do assignment this way:
你可以这样做:
key <- "a"
mylist[[key]] <- 5
mylist
## $a
## [1] 5
##
## $b
## NULL
##
## $c
## NULL
#2
3
You can also use a more R-like soltution, and use lapply
:
你也可以使用更像R的溶液,并使用lapply:
get_model = function(model_name) {
my_probs <- testPred[[model_name]]$Y1
return(roc(Ytst, my_probs))
}
model_list = lapply(names(my_models), get_model)
Note that this solution saves you a lot of boilerplate code, it also does not suffer from the reallocation problem of your solution by growing the object. For large datasets, this can mean that the lapply
solution is thousands of times faster.
请注意,此解决方案为您节省了大量样板代码,它也不会因为增长对象而导致解决方案的重新分配问题。对于大型数据集,这可能意味着lapply解决方案的速度提高了数千倍。
#3
2
I found the answer on this thread.
我在这个帖子上找到了答案。
I can grow a list using the following generic formula:
我可以使用以下通用公式来生成列表:
mylist <- list()
for (key in my_keys){
mylist[[ key ]] <- value # value is computed dynamically
}
In my OP:
在我的OP中:
-
mylist
ismy_rocs
-
key
ismodl
-
value
ismy_roc
mylist是my_rocs
键是modl
值是my_roc
#1
13
Generally in R, growing objects is bad. It increases the amount of memory used over starting with the full object and filling it in. It seems you know what the size of the list should be in advance.
通常在R中,生长对象很糟糕。它会增加从完整对象开始并填充它所使用的内存量。看来你应该提前知道列表的大小。
For example:
my_keys <- letters[1:3]
mylist <- vector(mode="list", length=length(my_keys))
names(mylist) <- my_keys
mylist
## $a
## NULL
## $b
## NULL
## $c
## NULL
You can do assignment this way:
你可以这样做:
key <- "a"
mylist[[key]] <- 5
mylist
## $a
## [1] 5
##
## $b
## NULL
##
## $c
## NULL
#2
3
You can also use a more R-like soltution, and use lapply
:
你也可以使用更像R的溶液,并使用lapply:
get_model = function(model_name) {
my_probs <- testPred[[model_name]]$Y1
return(roc(Ytst, my_probs))
}
model_list = lapply(names(my_models), get_model)
Note that this solution saves you a lot of boilerplate code, it also does not suffer from the reallocation problem of your solution by growing the object. For large datasets, this can mean that the lapply
solution is thousands of times faster.
请注意,此解决方案为您节省了大量样板代码,它也不会因为增长对象而导致解决方案的重新分配问题。对于大型数据集,这可能意味着lapply解决方案的速度提高了数千倍。
#3
2
I found the answer on this thread.
我在这个帖子上找到了答案。
I can grow a list using the following generic formula:
我可以使用以下通用公式来生成列表:
mylist <- list()
for (key in my_keys){
mylist[[ key ]] <- value # value is computed dynamically
}
In my OP:
在我的OP中:
-
mylist
ismy_rocs
-
key
ismodl
-
value
ismy_roc
mylist是my_rocs
键是modl
值是my_roc