Green Files

Error in file(file, "rt") : cannot open the connection

Day: 06/02/2022 - Time: 16:17:39

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.

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

Solution

You need to change directory <- ("./specdata") to directory <- ("./specdata/")

Relative to your current working directory, you are looking for the file 001.csv, which is in your specdata directory.

Reference:

https://stackoverflow.com/questions/27466317/error-in-filefile-rt-cannot-open-the-connection

GO UP
GO TO INDEX