Skip to content

Instantly share code, notes, and snippets.

@mGalarnyk
Last active January 2, 2017 16:11
Show Gist options
  • Save mGalarnyk/1cb0944166b79a0c78e58540c4268df0 to your computer and use it in GitHub Desktop.
Save mGalarnyk/1cb0944166b79a0c78e58540c4268df0 to your computer and use it in GitHub Desktop.
pollutantmean.R This file is used for the John Hopkins Data Science Specialization (R Programming). This file is posted for the blog post reviewing the specialization https://medium.com/@GalarnykMichael/in-progress-review-course-2-r-programming-jhu-coursera-ad27086d8438#.ui3hb8n46
pollutantmean <- function(directory, pollutant, id = 1:332) {
# Format number with fixed width and then append .csv to number
fileNames <- paste0(directory, '/', formatC(id, width=3, flag="0"), ".csv" )
# Reading in all files and making a large data.table
lst <- lapply(fileNames, data.table::fread)
dt <- rbindlist(lst)
if (c(pollutant) %in% names(dt)){
return(dt[, lapply(.SD, mean, na.rm = TRUE), .SDcols = pollutant][[1]])
}
}
# Example usage
pollutantmean(directory = '~/Desktop/specdata', pollutant = 'sulfate', id = 20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment