要替换的项目数不是替换长度的倍数

时间:2016-04-13 13:44:16

标签: r

我尝试使用此代码生成一系列日期/小时,但我一直收到错误

  

" all_dates [cycle,1]< - paste(c_dates [a]," - ",c_hours [b],   " h"):要替换的项目数量不是替换项目的倍数   长度"

使用的代码是:

 years <- c(2009, 2010, 2011, 2012, 2013) #1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006)
    months <- c(6, 7, 8, 9)


for (i in years)
{

      upper.bound<- paste("01", months[1], i, sep="-")
      lower.bound <- paste("30", months[4], i, sep="-")

      c_dates <- seq(as.Date(upper.bound, "%d-%m-%Y"), as.Date(lower.bound, "%d-%m-%Y"), "days")
      c_hours <- c(0, seq(0:22))

      len <- dim(as.matrix(c_dates))[1]*dim(as.matrix(c_hours))[1]

      all_dates <- data.frame()
      all_dates <- seq(0,0,length.out=len)

      dim(all_dates) <- c(dim(as.matrix(c_dates))[1]*dim(as.matrix(c_hours))[1], 1)

      cycle <- 1

      for(a in c_dates)
      {
        for(b in c_hours)
        {
          all_dates[cycle, 1] <- paste(c_dates[a], "-", c_hours[b], "h")
          cycle <- cycle + 1
        }
      }
}

有什么可能出错的想法吗?

1 个答案:

答案 0 :(得分:1)

您在for循环中使用向量项进行子集化。

替换

BorderBrush

通过

   all_dates[cycle, 1] <- paste(c_dates[a], "-", c_hours[b], "h")

或者将你的循环更改为:

   all_dates[cycle, 1] <- paste(a, "-", b, "h")