-
-
Save oskar-j/986df6f876af754edef2 to your computer and use it in GitHub Desktop.
Correlation between sulfate and nitrate in pollution monitors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
corr <- function(directory, threshold = 0) { | |
temp = list.files(path=paste("./",directory,sep=''), | |
pattern="*.csv", full.names = TRUE) | |
data = do.call("rbind", lapply(temp, function(x) read.csv(x))) | |
data$ID <- as.numeric(data$ID) | |
data <- data[complete.cases(data), ] | |
corrs <- as.numeric(c()) | |
for (i in unique(data$ID)){ | |
complete <- data[ which( data$ID == i ) , ] | |
if (nrow(complete) > threshold) | |
corrs <- append(corrs, cor(complete$sulfate, complete$nitrate)) | |
} | |
corrs | |
} | |
pollutantmean <- function(directory, pollutant, id = 1:332) { | |
temp = list.files(path=paste("./",directory,sep=''), | |
pattern="*.csv", full.names = TRUE) | |
data = do.call("rbind", lapply(temp, function(x) read.csv(x))) | |
data$ID <- as.numeric(data$ID) | |
if (pollutant == 'sulfate'){ | |
sub <- subset(data, ID %in% id & !is.na(sulfate), select = sulfate) | |
mean(sub$sulfate) | |
} else if (pollutant == 'nitrate') { | |
sub <- subset(data, ID %in% id & !is.na(nitrate), select = nitrate) | |
mean(sub$nitrate) | |
} else { | |
stop("Err: unknow column") | |
} | |
} | |
# counts complete readings for given set of monitors | |
complete <- function(directory, id = 1:332) { | |
temp = list.files(path=paste("./",directory,sep=''), | |
pattern="*.csv", full.names = TRUE) | |
data = do.call("rbind", lapply(temp, function(x) read.csv(x))) | |
data$ID <- as.numeric(data$ID) | |
data <- data[ which(complete.cases(data) & data$ID %in% id), c("ID")] | |
data <- as.data.frame(data) | |
colnames(data) <- c('occurences') | |
result <- aggregate(data, by=list(data$occurences), FUN=length) | |
colnames(result) <- c('id','nobs') | |
result[order(match(result$id,id)), ] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment