Commits

Ryan Tracey committed 141221a

week2 third task

Comments (0)

Files changed (1)

coursera/week2/corr.R

 corr <- function(directory, threshold = 0) {
-        ## 'directory' is a character vector of length 1 indicating
-        ## the location of the CSV files
+    ## '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
 
-        ## '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
 
-        ## Return a numeric vector of correlations
+    cordata <- c()
 
+    files <- list.files(directory)
+    for(file in files){
+        # get the file and its data
+        file <- sprintf("%s/%s", directory, file)
+        data <- read.csv(file)
 
+        # get the complete data (and count) for that file
+        good <- data[complete.cases(data),]
+        rows <- nrow(good)
+
+        # only calc correlation on files with more than
+        # 'threshold' complete rows
+        if(rows > threshold){
+            correlation <- cor(good$sulfate, good$nitrate)
+            cordata <- append(cordata, correlation)
+        }
+    }
+
+    # if no data (no files with number of rows > threshold
+    if(length(cordata) < 1){
+        cordata <- vector(mode="numeric", length=0)
+    }
+
+    # finished
+    return(cordata)
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.