Skip to content

Commit

Permalink
Assignment-1-Part-3
Browse files Browse the repository at this point in the history
Solution
  • Loading branch information
Rajesh Subramanian committed Sep 15, 2014
1 parent fa1319a commit 5d98c1b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions programming-with-r/assignments/assignment-1-air-pollution/corr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
corr <- function(directory, threshold = 0) {
## 'directory' is a character vector of length 1 indicating
## the location of the CSV files

## 'threshold' is a numeric vector of length 1 indicating the
## number of completely observed observations (on all
## variables) required to compute the correlation between
## nitrate and sulfate; the default is 0

## Return a numeric vector of correlations
source("complete.R")
completeCases <- complete(directory)
casesAboveThreshold <- completeCases[completeCases$nobs > threshold,1]
allFiles <- list.files(path = directory, full.names = TRUE)
correlations <- rep(NA,length(casesAboveThreshold))
for (i in casesAboveThreshold) {
fileData <- (read.csv(allFiles[i]))
completeCases <- complete.cases(fileData)
validSulfateData <- fileData[completeCases, 2]
validNitrateData <- fileData[completeCases, 3]
correlations[i] <- cor(x = validSulfateData, y = validNitrateData)
#print(head(fileData[completeCases,]))
#print(validSulfateData)
#print(validNitrateData)
}
correlations <- correlations[complete.cases(correlations)]
}

0 comments on commit 5d98c1b

Please sign in to comment.