'In R, nested loops don't return the expected result while reading through multiple files

Objective: calculate correlation coefficients between 2 variables for multiple files that meet the custom criterion for complete cases (threshold argument).

Data: 332 .csv files in a specified directory.

Desired output: vector with correlation coefficients for the files.

Code:

 correl <- function(directory = "~/specdata/specdatacsv", threshold = 0) {
  filelist <- list.files(path = directory, pattern = ".csv", full.names = TRUE)
  nobs <- numeric()
  corrvector <- numeric()
  
  for(i in length(filelist)) {
    data <- read.csv(filelist[i])
    nobs <- sum(complete.cases(data))
    if (nobs <= threshold) { next
    } else {
      nitrate <- as.vector(data$nitrate)
      sulfate <- as.vector(data$sulfate)
      goodSulfate <- complete.cases(sulfate)
      goodNitrate <- complete.cases(nitrate)
      icorr <- cor(goodNitrate, goodSulfate)
      corrvector <- c(corrvector, icorr)
    }
  }
  corrvector
}

The output for the threshold 150 should return:

[1] -0.01895754 -0.14051254 -0.04389737 -0.06815956 -0.12350667 -0.07588814

But instead the empty corrvector gets returned. Please, help me find the mistake I've made.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source