【发布时间】:2018-03-14 16:20:09
【问题描述】:
我收到此错误:
$ 运算符对原子向量无效
当我运行这个脚本时:
require(coefplot)
filenames <- list.files(path = '/Documents/R/data/', pattern = "account_exp_10_sb_sql__[0-9]{2,3}-[0-9]{2,3}.csv", full.names = TRUE)
analyze <- function(filename){
fm_1 <- NULL
dx_1 <- NULL
cat('reading: ', filename)
dx_1 <- read.csv(filename)
head(dx_1)
fm_1 <- lm(default_perc ~ credit_score + email + credit_card_pmt, data = dx_1)
return(fm_1)
}
cur_fm <- NULL
ct <- 1
fm_list <- list()
for (fn in filenames)
{
#cat(ct, ' ', fn)
cur_fm <- analyze(fn)
summary(cur_fm)
fm_list$ct <- cur_fm
ct <- ct + 1
#stop()
}
#fm_list
multiplot(plotlist = fm_list)
该脚本应读取 12 个 csv 文件,对每个文件运行 lm(),尝试将结果存储在列表中,然后在列表上执行多图。
我尝试了fm_list$ct 和fm_list[[ct]],但我得到了同样的错误。
此外,摘要不会打印出来。我不知道为什么它不起作用。
【问题讨论】:
-
我想你想要
fm_list[[fn]] <- cur_fm,如果你想在for循环的每次迭代中附加列表。 -
我刚刚尝试了这两种方法,但仍然出现错误。
-
我不知道这是否重要,但我使用的是 RStudio Windows 1.1.423 和 R 3.4.3 x86_64。
-
我认为它发生在这一行:fm_list$ct
-
我可以重现您的问题。
summary是隐藏的,如果我使用fm_list[[ct]] <- cur_fm,错误消息是由最后 (!) 行 (multiplot) 引起的