Skip to content

Instantly share code, notes, and snippets.

@nharrell04
Created June 10, 2016 03:20
Show Gist options
  • Save nharrell04/cf8b79f4e9c615bf91f58d4f3b7e5e8a to your computer and use it in GitHub Desktop.
Save nharrell04/cf8b79f4e9c615bf91f58d4f3b7e5e8a to your computer and use it in GitHub Desktop.
pollutantmean <- function(directory, pollutant, id = 1:332) {
files <- sprintf("%s/%03d.csv", directory, id)
measurements <- c()
for (file in files) {
frame <- read.csv(file)
file_measurements <- frame[[pollutant]]
measurements <- c(measurements, file_measurements)
}
mean(measurements, na.rm = TRUE)
}
complete <- function(directory, id = 1:332){
counts <- data.frame("id","nobs")
colnames(counts) <- c("id", "nobs")
for (i in id) {
file <- sprintf("%s/%03d.csv", directory, i)
frame <- read.csv(file)
count <- length(frame[!is.na(frame$sulfate) & !is.na(frame$nitrate),]$ID)
print(count)
counts[id,]$id <- id
counts[id,]$nobs <- count
## need to figure out how to append a row to a data frame
}
counts
}
corr <- function(directory, threshold = 0) {
files <- list.files(directory, full.names = TRUE)
corrs <- c()
for (file in files) {
frame <- read.csv(file)
complete <- frame[!is.na(frame$sulfate) & !is.na(frame$nitrate),]
count <- length(complete$ID)
if (count > threshold){
corrs <- c(corrs, cor(complete$nitrate, complete$sulfate))
}
}
corrs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment