Purpose Air Pollution Cursor

Using Mac OS 10.10.3 RStudio Version 0.98.1103

My working directory is a list of 332 CSV files and I installed it correctly. Here's the 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 the challenge is to get the average of the first 10 numerical values ​​of the pollutant. For this I used a call function (where "spectata" is a directory with 332 CSV files):

pollutantmean(specdata, "Nitrate", 1:10)

      

Error messages appear:

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

** Optional: Warning message: In file (file, "rt"): file cannot be opened "NA": no such file or directory

Like many students who have asked questions here, I am new to programming and R and still far from getting any results when calling my function. There are many questions and answers about this coursera assignment in Stack Overflow, but my review of these exchanges did not address the bug in my code.

Anyone have a suggestion to fix the error?

+3


source to share


5 answers


In addition to the other answers, you can try this:

all_files <- list.files(directory, pattern="*.csv", full.names = TRUE)

      

to avoid selecting any other file.



or even this strange

all_files <- paste(directory, "\\", sprintf("%03d", id), ".csv", sep="")

      

+1


source


I am wasting time answering since the question is returned in every Coursera session.

First, be careful with the typo: call pollutantmean("specdata", "Nitrate", 1:10)

instead pollutantmean(specdata, "Nitrate", 1:10

.



Then your working directory should be the parent "specdata" directory (for example, if your path was / dev / specdata, your working directory should be / dev).

You can get the current working directory from getwd()

and set a new one with setwd()

(be careful, the path will refer to the current working directory).

+1


source


Add a line after all_files <- list.files(directory, full.names = TRUE)

(it's a bad habit to use T

instead TRUE

):

print(all_files)

      

Then call your function again so you can see the contents of that object. Then check where you are working from getwd()

.

0


source


Change your line. 5 to dat <- rbind(dat, read.csv(i, comment.char = ""))

This will link the data of all csv files to the dat data file.

0


source


Based on the information provided, it can be assumed that there are no 332 files in the directory you specified (if you try to access a vector index that is out of scope, NA is returned, hence the error "cannot open file" NA "). thought that the path you are using (which is not provided) points to a directory that does not contain csv files (there are supposedly 332 files in that directory). Some suggestions:

  • Make sure the directory you provide is accurate. Just run the file list.files to see what files exist in the directory you are using.
  • Use the list.files template argument to make sure you are only going to read the csv files
  • Loop through the files using the length of the vector returned from list.files instead of manually encode it
  • You can add a health check to make sure you read all files by printing each file or returning a list containing the results and filenames
-1


source







All Articles