I have a list of files (from 1 to 332) inside my directory. The file1 corresponds to id1, and the file2 corresponds to id2, and so on and so forth.
Each file contains 4 columns, and I have to calculate the sums and lengths of the 2th column (labelled as "pollutant") by ignoring the NAs.
I have tried everything: !is.na(file), na.rm = TRUE, omit...It works when I want the sum and length from 1:100 or 1:60 (from the value 1 to another value), but it doesn't work from 70:72 for instance. I can't pin point the problem.
Here is the part of my code that deals with it:
pollutantmean <- function(directory,pollutant,id= 1:332){
files <- list.files(directory)
sums <- numeric (length(id))
lengths <- numeric (length(id))
means <- numeric (length(id))
for (i in id){
file <- read.csv(files[i])[,pollutant]
sums[i] <- sum(file,na.rm = TRUE)
lengths[i] <-length(file[!is.na(file)])
}
means <-(sum(sums)/sum(lengths))
return(list(sums, lengths, means))
}
Thanks in advance for your help!