r - 文件中的错误(文件, "rt"): cannot open the connection

标签 r csv

我是 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

最佳答案

您需要更改directory <- ("./specdata")directory <- ("./specdata/")

相对于您当前的工作目录,您正在查找文件 001.csv,该文件位于您的specdata 目录中。

如果没有任何上下文,这个问题几乎不可能回答,因为您没有在这里向我们提供您的工作目录的结构。幸运的是,我已经在 Coursera 上学习了 R 编程,所以我已经做了这个作业问题。

关于r - 文件中的错误(文件, "rt"): cannot open the connection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27466317/

相关文章:

Excel for Mac 2016 - 另存为 CSV - 行尾错误

python - Pandas read_csv 中的段错误

r - 如何在 for 循环中使用 mutate() 和 case_when()?

r - 循环并为子组寻找最近的数据点

excel - 导入包含多行单元格的制表符分隔文本文件(到 Excel 或 Access)

python - 如何使用 Pandas 在 python 中显示 CSV 文件中的列?

javascript - meteor CSV 导出

r - 使用 R 组合所有行对

r - 重复提示标签删除 : importing phylogenetic tree in R for comparison

r - 页眉中的图片/ Logo 四开 qmd 转 pdf