Error in file(file, “rt”) : cannot open the connection [duplicate]

This question already has answers here: How to open CSV file in R when R says “no such file or directory”?

I’m new to R, and after researching this error extensively, I’m still not able to find a solution for it. Here’s the code. I’ve checked my working directory, and made sure the files are in the right directory. Appreciate it. Thanks

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))
            }
} 

The error I’m getting is below:

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

Leave a Comment