在R中使用变量名生成列表 [英] Growing a list with variable names in R

查看:404
本文介绍了在R中使用变量名生成列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在R中增加一个列表,其中每个条目的值和名称都保存在一个变量中,但是似乎不起作用.

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.

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_rocs最后还是空的,即使我知道循环会迭代(my_roc已填充)为什么?

My list my_rocs is empty at the end, even though I know that the loop iterates (my_roc is filled in) Why?

在相关说明中,有没有办法做到这一点而无需循环?

On a related note, is there a way to do this without looping?

推荐答案

我在我可以使用以下通用公式来生成列表:

I can grow a list using the following generic formula:

mylist <- list()

for (key in my_keys){ 
mylist[[ key ]] <- value # value is computed dynamically
}

在我的OP中:

  • mylistmy_rocs
  • keymodl
  • valuemy_roc
  • mylist is my_rocs
  • key is modl
  • value is my_roc

这篇关于在R中使用变量名生成列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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