Skip to content

Instantly share code, notes, and snippets.

@byanuaria
Last active July 23, 2018 15:37
Show Gist options
  • Save byanuaria/035f0f6bbbc3fd67f03849f03282be63 to your computer and use it in GitHub Desktop.
Save byanuaria/035f0f6bbbc3fd67f03849f03282be63 to your computer and use it in GitHub Desktop.
# problem 1
# part 1
pollutantmean <- function(directory, pollutant, id = 1:332) {
filelist <- list.files(path = directory, pattern = ".csv",
full.names = TRUE)
values <- numeric()
for(i in id) {
data <- read.csv(filelist[i])
values <- c(values, data[[pollutant]]) # sulfate or nitrate
}
mean(values, na.rm = TRUE)
}
pollutantmean("specdata/", "nitrate", 70:72)
# part 2
complete <- function(directory, id = 1:332) {
filelist <- list.files(path = directory, pattern = ".csv",
full.names = TRUE)
# create empty vectors to later be cbind'd into a data frame
ide <- numeric()
nobs <- numeric()
for(i in id) {
# reads the data directory
data <- read.csv(filelist[i])
# id numbers chosen gets put into the vector
ide <- c(ide, id[i])
# finds all the values that are not NA and assigns to nobs vector
nobs <- c(nobs, sum(!is.na(data[[2]])))
}
data.frame(cbind(id, nobs)) # vectors now with data, changed to DF
}
# part 3
@byanuaria
Copy link
Author

test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment