Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
16 lines (15 sloc)
805 Bytes
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
# takes a directory of data files and a threshold for complete cases and calculates the correlation | |
#between sulfate and nitrate for monitor locations where the number of completely observed cases (on all variables) | |
#is greater than the threshold. The function should return a vector of correlations for the monitors that meet the threshold requirement. | |
#If no monitors meet the threshold requirement, then the function should return a numeric vector of length 0. | |
corr <- function(directory, threshold = 0) { | |
FL<- list.files(path = directory, pattern = ".csv", full.names = TRUE) | |
cors <- numeric() | |
for (i in 1:332) { | |
dat <- read.csv(FL[i]) | |
if (sum(complete.cases(dat)) > threshold) { | |
cors <- c(cors, cor(dat[["sulfate"]], dat[["nitrate"]], use = "complete.obs")) | |
} | |
} | |
cors | |
} |