Skip to content

Instantly share code, notes, and snippets.

@wmeister-old
Created April 3, 2016 19:20
Show Gist options
  • Save wmeister-old/80099905c0687ecc76c59cbdaaf8e088 to your computer and use it in GitHub Desktop.
Save wmeister-old/80099905c0687ecc76c59cbdaaf8e088 to your computer and use it in GitHub Desktop.
pollutantmean <- function(directory, pollutant, id = 1:332) {
files <- list.files(directory, full.names=T)
frame <- read.csv(files[1])
#for(file in files[2:3]) { # XXX DELETE ME
for(file in files[2:length(files)]) {
frame <- rbind(read.csv(file), frame)
}
#frame <- frame[1:1000,] # XXX delete me!!
F = is.element(frame$ID, id)
frame <- frame[F,]
mean <- mean(frame[[pollutant]], na.rm = T)
round(mean, 3) # XXX REMOVE SAID NOT TO ROUND
}
# rows <- frame$ID[is.element(frame$ID, id)]
#
# mean(if(pollutant == "sulfate") {
# frame$sulfate
# } else {
# frame$nitrate
# }
# , na.rm = T)
#}
print(pollutantmean("d:/gdrive/src/datasciencecoursera/specdata", "sulfate", 1:10))
print(pollutantmean("d:/gdrive/src/datasciencecoursera/specdata", "nitrate", 70:72))
print(pollutantmean("d:/gdrive/src/datasciencecoursera/specdata", "nitrate", 23))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment