R studio doesn't find objects in my function

Viewed 1433

I’m new to programming and I’m currently writing a function to go through hundreds of csv files in the working directory.

The files have tons of NA values in it.

The function (which I call it corr) has two parameters, the directory, and a threshold value (numeric vector of length 1 indicating the number of complete cases).

The purpose of the function is to take the complete cases for two columns that are sulfate and nitrate(second and third column in the spreadsheet) and calculate the correlation between them if the number of complete cases is greater than the threshold parameter.

The function should return a vector with the correlation if it met the threshold requirement (the default threshold value is 0).

When I run the code I get back two of the following:

  1. A + sign in the console

OR

2.The objects I created in the function can't be found.

Any help would be much appreciated. Thank you in advance!

corr <- function(directory, threshold=0){
  filelist2<- data.frame(list.files(path=directory, 
                                    pattern=".csv", full.names=TRUE))            

  corvector <- numeric()

  for(i in 1:length(filelist2)){
    data <-data.frame(read.csv(filelist2[i]))
    removedNA<-complete.cases(data)
    newdata<-data[removedNA,2:3] 


    if(nrow(removedNA) > threshold){
      corvector<-c(corvector, cor(data$sulfate, data$nitrate )) 


    }


  }
  corvector   
}
1 Answers
Related