. Advertisement .
..3..
. Advertisement .
..4..
I get the error: Error in file(file, “rt”) : cannot open the connection when I try to run the program below:
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 appears the system notifies as follows:
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
I tried a few ways, but it didn’t work. Can someone suggest a working solution to solve this problem? Thanks!
For your question, I just faced the same error and corrected by removing spacing in address using paste0 instead of paste
Another option is to open the csv file by providing the full path of the file and not accessing the directory in question:
Cause of the warning message : Maybe windows generated a name of ./datatest001.csv.csv not ./datatest001.csv
You need to change directory, from “./datatest” changed to “./datatest/”