r - coursera air pollution assignment -
using mac os 10.10.3 rstudio version 0.98.1103
my working directory list of 332 .csv files , set correctly. here's code:
pollutantmean <- function(directory, pollutant, id = 1:332) { all_files <- list.files(directory, full.names = t) dat <- data.frame() for(i in id) { dat <- rbind(dat, read.csv(all_files[i])) } ds <- (dat[, pollutant], na.rm = true) mean(ds[, pollutant]) }
part of assignment mean of first 10 numeric values of pollutant. this, used call function (where "spectata" directory 332 .csv files):
pollutantmean(specdata, "nitrate", 1:10)
the error messages are:
**error in file(file, "rt") : cannot open connection
** in addition: warning message: in file(file, "rt") : cannot open file 'na': no such file or directory
like many students have posed questions here, i’m new programming , r , still distant getting results when calling function. there many questions , answers coursera assignment in stack overflow review of these exchanges hasn't addressed bug in code.
anyone have suggestion how fix bug?
in addition other answers can try this:
all_files <- list.files(directory, pattern="*.csv", full.names = true)
to avoid select other kind of file.
or strange one
all_files <- paste(directory, "\\", sprintf("%03d", id), ".csv", sep="")
Comments
Post a Comment