0
votes

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

1
Can you edit your question to include the output of args(cor)? My guess is you have redefined cor unintentionally. - Thomas
Cannot reproduce. In addition to Thomas' suggestion, please post your OS, & R version. - Carl Witthoft
thanks for the input I included the info in the edit - stan
rm(cor) and try again. you named corr cor on an earlier attempt - rawr
I suspect you are using a different cor function (from a different package). What does environment(cor) return? For most of us it is : <environment: namespace:stats> - IRTFM

1 Answers

2
votes

There is no parameter named na.rm in function cor. You should substitute your code as:

use = "complete.obs"