error: replacement has 1 row, data has 0

error: replacement has 1 row, data has 0

corr <- function(directory, threshold = 0) {
  setwd("c:/users/hp1/desktop")
  files_full <- list.files(directory,full.names = TRUE)
  cr <- data.frame()
  j = 1
  for(i in 1:332)
  {
    y <- read.csv(files_full[i])
    z <- y[complete.cases(y),]

    if (nrow(z) > threshold){
      cr[j] <- cor(z[,'sulfate'], z[,'nitrate'], method = "pearson")

      j = j+1
      }


    }
  cr

  }

显示以下错误: [<-.data.frame(*tmp*, j, value = -0.222552560758546) 错误: 替换有 1 行,数据有 0 我期望随着 j 的增加,值会被添加到 cr dtaframe 中。然而,这并没有发生。 请提出必要的修改建议

你可以尝试这样的事情。如果您提供可重现的示例,我可以向您展示如何清理结果。 sapply 将尝试简化结果,但您可以通过指定 simplify = FALSE 并删除不需要的列表元素来停止它。

setwd("c:/users/hp1/desktop") # I would use this outside a function

corr <- function(directory, threshold = 0) {
  files_full <- list.files(directory, full.names = TRUE)

  sapply(files_full, FUN = function(x) {
    y <- read.csv(x)
    z <- y[complete.cases(y),]

    if (nrow(z) > threshold){
      out <- cor(z[,'sulfate'], z[,'nitrate'], method = "pearson")
    } else {
      return(NA) # or some other way you want to handle the result
    }
  })
}