如何从列表中获取型号名称?

问题描述

让我们考虑两个模型和由它们创建的列表:

set.seed(42)
y <- rnorm(100)
x <- runif(100)
z <- rexp(100)
model_1 <- lm(y ~ x)
model_2 <- lm(y ~ z)

model_list <- list(model_1,model_2)

如何从 model_list获取其中的模型名称?即以某种方式输出 "model_1","model_2"。我尝试使用 substitute(model_list[[1]]) 但它没有解决问题。你知道它是如何获得的吗?

编辑

我尝试使用评论中提出的代码

x <- sapply(ls(),function(x) if(is.function(x)) NA else tracemem(get(x)))

但是我遇到了错误

Error in tracemem(get(x)) : argument must not be a function

你知道怎么解决吗?

编辑 2

set.seed(42)
y <- rnorm(100)
k <- runif(100)
z <- rexp(100)
model_1 <- lm(y ~ k)
model_2 <- lm(y ~ z)
model_list <- list(model_1,model_2)



x <- sapply(ls(),function(x) if(is.function(x)) NA else tracemem(get(x)))

Error in tracemem(get(x)) : argument must not be a function

你能看看它是否适合你吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)