-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
programming-with-r/assignments/assignment-1-air-pollution/corr.R
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
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)] | ||
} |