ASw*_*ine 5 r rstudio read.csv
我在 Windows 7 上运行 rStudio v3.1.2。这台笔记本电脑是 64 位机器。
我正在参加 Coursera 提供的 JHU R 编程课程,但我在问题的第 1 部分中遇到了一个错误。我有一些错误处理函数,我没有考虑到这个例子,所以我真的只是想展示我绝对需要的东西。我包含这些消息的唯一原因是证明必须满足所有条件才能继续。
pollutantmean <- function(directory, pollutant, id=1:332) {
setwd("C:\\Users\\WR-eSUB\\specdata")
if(!isValidDirectory(directory)) {
stop("Invalid input given. Please specify valid directory to operate on.")
}
if(!isValidPollutant(pollutant)) {
stop("Invalid input given. Please specify valid pollutant (nitrate/sulfate).")
}
if(!isValidIdRange(id)) {
stop("Invalid input given. Please specify valid id range (1:332).")
}
sortedData = numeric()
for (i in id) {
thisFileName = paste(formatC(i, width = 3, flag = "0"), ".csv", sep="")
thisFileRead = read.csv(directory, thisFileName)
sortedData = c(sortedData, thisFileRead[[pollutant]])
}
mean(sortedData, na.rm = TRUE)
}
Run Code Online (Sandbox Code Playgroud)
请注意,WR-eSUB 中有一个名为 specdata 的文件夹,该文件夹中有一个包含 .csv 文件的目录,也称为 specdata。我可以改变这一点,但到目前为止我一直在使用它,我没有遇到任何问题。
当我打电话时,pollutantmean("specdata", "nitrate", 1:2)我收到以下错误消息:
Error in file(file, "rt") : cannot open the connection
In addition: Warning message: In file(file, "rt") : cannot open file 'specdata': Permission denied
Run Code Online (Sandbox Code Playgroud)
现在,在我尝试完成这部分作业的无数次尝试中,我已经能够使用 lapply 之类的东西以其他方式提取数据,但是因为我一直被卡住,所以我把所有东西都扔掉了,想以这种方式尝试。
我在网上搜索过试图找到这个解决方案。尽管事实上有几个问题得到了答复,但似乎没有一个像这个问题一样令人困惑。WR-eSUB 是一个管理文件夹,但之前尝试打开其中的文件并没有产生此错误。
睡个好觉后,我发现了问题。我根本没有使用目录,所以我需要添加它。
thisFileName = paste(directory, "/", formatC(i, width = 3, flag = "0"), ".csv", sep="")
Run Code Online (Sandbox Code Playgroud)