【发布时间】:2014-05-16 19:57:31
【问题描述】:
我有一个函数定义一个目录和一个要加载和分析的文件。
该函数正在读取这些值,但是当被read.csv 调用时,它似乎无法识别文件名。我认为这是格式的问题,但我不知道。除了 dt Selectdata 从未创建 Selectdata<-read.csv(z, header=TRUE) 之外,一切似乎都在做它应该做的事情。 z 是一个包含要加载的文件名的向量。
调试时这是生成的错误:
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
'file' must be a character string or connection
由于错误表示正在读取的值不是字符、字符串或连接,我厌倦了设置它as.character 等但无济于事。它一定是我缺少的更简单的东西。
运行时:
Warning message:
In mean.default(Selectdata$polutant, na.rm = TRUE) :
argument is not numeric or logical: returning NA
但是,实际上,Selectdata 从未被创建(因此,尝试了均值,但显然没有任何价值)
testfun <- function(directory, polutant, id) {
setwd(directory)
x <- polutant # not needed just checking to see if polutant has been read
print(x) # not needed just checking
y <- list.files(directory, full.names=TRUE)
print(y[id]) # not needed just checking
z <- y[id]
if (length(id == 1)) {
Selectdata <- read.csv(z, header = TRUE)
}
mean(Selectdata$polutant, na.rm=TRUE)
}
【问题讨论】:
-
那么您的
print(y[id])行中打印的是什么? (不过还有更多错误) -
如果您在堆栈溢出中搜索“[r] 污染物”,您可以看到您的同学提出的所有问题并使用其中一些答案。
-
谢谢 MrFlick - 感谢您对我们的入侵。我希望有一天我能回报您的青睐