将对象存储到列表中并为其命名,存储在 R 中的字符向量中

Storing an object to a list and giving it a name stored in a character vector in R

我是 运行 glm 分类器,我想将模型存储在命名列表中,为每个模型提供分类器中使用的预测器的名称——从字符向量中提取。但是我收到一个错误。

对于可重现的示例,我使用 mtcars 数据集(基础 R):

> data(mtcars)
> results <- list()
> model1 <- glm(am ~ hp, mtcars, family ="binomial")
> results <- list()
> names <- c("hp"  , "cyl")
> results <- append(results, list(names[1] = model1))
Error: unexpected '=' in "results <- append(results, list(names[1] ="

我们将不胜感激您的建议。

将最后一行替换为

results[[names[1]]] <- model1