R - “$ operator is invalid for atomic vectors”错误信息
R - "$ operator is invalid for atomic vectors" error message
我收到此错误:
$ operator is invalid for atomic vectors
当我 运行 这个脚本时:
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]]
,但我得到了同样的错误。
此外,摘要没有打印出来。我不明白为什么它不起作用。
您的代码存在三个问题:
将函数 return 值存储在列表中
调用 multiplot
函数的错误方式(没有 plotlist
参数 - 参见 ?multiplot
.
summary
只有在任何范围之外时才会打印到控制台
代码块(R 是一种脚本语言)。如果将其放入代码块(此处:for
函数),则必须使用 print
解决方法是:
# ... your code as above
cur_fm <- NULL
ct <- 1
fm_list <- list()
for (fn in filenames)
{
cat(ct, ' ', fn)
cur_fm <- analyze(fn)
print(summary(cur_fm)) # 3. print required inside a code block
fm_list[[fn]] <- cur_fm # 1. use the file name as list item name
ct <- ct + 1
#stop()
}
# 2. Pass all lm results in the list in "..." argument of multiplot
# do.call requires named list elements since they are used to find
# the corresponding function arguments. If there is no match
# the list element is put into the "..." argument list
do.call(multiplot, fm_list)
请注意,该解决方案存在一些错误风险,例如。 G。如果您的文件名与 multiplot
函数的形式参数名称相同。
你可以通过e来避免这个风险。 G。添加不属于任何参数名称的前缀:
fm_list[[paste0("dot_dot_dot_", fn)]] <- cur_fm
我收到此错误:
$ operator is invalid for atomic vectors
当我 运行 这个脚本时:
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]]
,但我得到了同样的错误。
此外,摘要没有打印出来。我不明白为什么它不起作用。
您的代码存在三个问题:
将函数 return 值存储在列表中
调用
multiplot
函数的错误方式(没有plotlist
参数 - 参见?multiplot
.summary
只有在任何范围之外时才会打印到控制台 代码块(R 是一种脚本语言)。如果将其放入代码块(此处:for
函数),则必须使用print
解决方法是:
# ... your code as above
cur_fm <- NULL
ct <- 1
fm_list <- list()
for (fn in filenames)
{
cat(ct, ' ', fn)
cur_fm <- analyze(fn)
print(summary(cur_fm)) # 3. print required inside a code block
fm_list[[fn]] <- cur_fm # 1. use the file name as list item name
ct <- ct + 1
#stop()
}
# 2. Pass all lm results in the list in "..." argument of multiplot
# do.call requires named list elements since they are used to find
# the corresponding function arguments. If there is no match
# the list element is put into the "..." argument list
do.call(multiplot, fm_list)
请注意,该解决方案存在一些错误风险,例如。 G。如果您的文件名与 multiplot
函数的形式参数名称相同。
你可以通过e来避免这个风险。 G。添加不属于任何参数名称的前缀:
fm_list[[paste0("dot_dot_dot_", fn)]] <- cur_fm