我是R语言的新手,尽管经过了大量的调查研究,但仍然无法找到解决这个错误的方法。以下是代码。我已经检查了我的工作目录,并确保文件在正确的目录中。谢谢。
pollutantmean <- function(directory, pollutant = "nitrate", id= 1:332)
{ if(grep("specdata",directory) ==1)
{
directory <- ("./specdata")
}
mean_polldata <- c()
specdatafiles <- as.character(list.files(directory))
specdatapaths <- paste(directory, specdatafiles, sep="")
for(i in id)
{
curr_file <- read.csv(specdatapaths[i], header=T, sep=",")
head(curr_file)
pollutant
remove_na <- curr_file[!is.na(curr_file[, pollutant]), pollutant]
mean_polldata <- c(mean_polldata, remove_na)
}
{
mean_results <- mean(mean_polldata)
return(round(mean_results, 3))
}
}
我遇到的错误如下:
Error in file(file, "rt") : cannot open the connection
file(file, "rt")
read.table(file = file, header = header, sep = sep, quote = quote,
dec = dec, fill = fill, comment.char = comment.char, ...)
read.csv(specdatapaths[i], header = T, sep = ",")
pollutantmean3("specdata", "sulfate", 1:10)
In addition: Warning message:
In file(file, "rt") :
cannot open file './specdata001.csv': No such file or directory
specdata
的文件夹,其中包含文件001.csv
等等。正确的路径是./specdata/001.csv
,而不是./specdata001.csv
。尝试在specdatapaths
行中将sep=""
更改为sep="/"
. - jlhoward[.data.frame
(curr_file, , pollutant) : undefined columns selected 6 stop("undefined columns selected") 5[.data.frame
(curr_file, , pollutant) 4 curr_file[, pollutant] 3[.data.frame
(curr_file, !is.na(curr_file[, pollutant]), pollutant) 2 curr_file[!is.na(curr_file[, pollutant]), pollutant] 1 pollutantmean("specdata", "sulfate", 1:10) - ldeassis