As an assignment for an R programming class, I have to write a function that takes a directory of data files and a threshold for complete cases and calculates the correlation between two columns (sulfate and nitrate) from each file where the number of completely observed cases is greater than the threshold. The function should return a vector of correlations for the monitors that meet the threshold requirement
I am running R version 3.1.1 (via R studio) on MACOSX 10.9.5 this is my code
corr <- function(directory, threshold = 0) {
filenames <- list.files(directory, pattern = "*.csv", full.names = TRUE)
cors <- numeric()
for (i in seq_along(filenames)) {
tempDF <- read.csv(filenames[i])
if(nrow(tempDF[complete.cases(tempDF),]) > threshold){
j <- length(cors) + 1
cors[j] <- cor(tempDF$sulfate, tempDF$nitrate, use = "na.or.complete")
}
}
return(cors)
}
the expected output for the code would look like:
cr <- corr("specdata", 150)
head(cr)
[1] -0.01895754 -0.14051254 -0.04389737 -0.06815956 -0.12350667 -0.07588814
however when I run cr on my code I get the error:
Error in cor(tempDF$sulfate, tempDF$nitrate, use = "na.or.complete") : unused argument (use = "na.or.complete")
I have tried various work arounds with to no avail. substituting na.or.complete for na.rm or complete.cases gives the same error. some advice would be really appreciated!
running args(cor) outputs: function (directory, threshold = 0) NULL
args(cor)? My guess is you have redefinedcorunintentionally. - Thomasrm(cor)and try again. you namedcorrcoron an earlier attempt - rawrcorfunction (from a different package). What doesenvironment(cor)return? For most of us it is :<environment: namespace:stats>- IRTFM