错误:替换有1行,数据有0

时间:2015-07-17 09:33:17

标签: r replace runtime-error

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,值= -0.222552560758546)出错:   替换有1行,数据有0 我期待j增量,值将被添加到cr dtaframe。然而,这没有发生。 请提出必要的修改

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情。如果您提供可重现的示例,我可以向您展示如何清理结果。 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
    }
  })
}