Skip to content

Instantly share code, notes, and snippets.

@Luckyeva
Last active February 9, 2016 04:03
Show Gist options
  • Save Luckyeva/be3c104d1dd59fe4c1d1 to your computer and use it in GitHub Desktop.
Save Luckyeva/be3c104d1dd59fe4c1d1 to your computer and use it in GitHub Desktop.
This is one of the functions I wrote for R programming Coursera assignment
# R assignment 1 - Air Poluttion
corr <- function(directory, threshold = 0){
# 'threshold' is a numeric vector of length 1 indicating the
# number of complete cases required to compute the correlation
setwd(paste("/Users/lucky1eva/Downloads/", directory, sep = '' ))
cr <- NULL
dlist <- complete(directory, 1:332)
oklist <- dlist[dlist[,2] >= threshold , 1]
if (length(oklist) != 0) {
for (i in 1:length(oklist)) {
if (oklist[i] < 10) {
idd[i] <- paste("00", oklist[i], sep = '')
} else if (oklist[i] >= 10 && oklist[i] < 100) {
idd[i] <- paste("0", oklist[i], sep = '')
} else {
idd[i] <- oklist[i]
}
}
} else {
return(cr <- vector(mode = "numeric", length = 0))
}
# read tables and compute the vector of cr in each selected table
for (k in 1:length(idd)) {
myTable <- read.csv(paste(idd[k], ".csv", sep = ''), header = TRUE)
if (!any(complete.cases(myTable))) {
next
} else
{cr[k] <- cor(myTable[, 2], myTable[, 3], use = "complete.obs")}
}
return (cr)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment