R执行报错:Error in `[<-.ts`(`*tmp*`,...only replacement of elements is allowed

Posted xianhan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R执行报错:Error in `[<-.ts`(`*tmp*`,...only replacement of elements is allowed相关的知识,希望对你有一定的参考价值。

原因:

pred$mean是Time-Series类型,rbind函数不支持。通过as.double将其转换成double类型即可。

修改前代码:

all_predata_time <- data.frame(pd=0.1,Row=1,preRow=0,pt=0.1,stasid=‘1‘,InitDate=‘1‘);
all_predata_time <- all_predata_time[-1,]

stasPowerPre_Time <- function(staid){
  testSrc<-......
  if(nrow(testSrc)>0)
  {
    maxDate <- max(testSrc$bizdate)
    tsd<- ts(testSrc$dp,start=c(2016,01,01),frequency=365)
    str(tsd)
    x <- ets(tsd)
    pred <- forecast(x,h=365)
    
    predata <- data.frame(pd = pred$mean)
    predata$Row <-as.integer(row.names(predata));
    predata$preRow <- predata$Row -1;
    predata$pt <- as.double(c(0));
    predata$stasid <- staid;
    predata$InitDate <- maxDate ;
    all_predata_time <<- rbind(all_predata_time,predata)
  }
}

  

修改后代码:

all_predata_time <- data.frame(pd=0.1,Row=1,preRow=0,pt=0.1,stasid=‘1‘,InitDate=‘1‘);
all_predata_time <- all_predata_time[-1,]

stasPowerPre_Time <- function(staid){
  testSrc<-......
  if(nrow(testSrc)>0)
  {
    maxDate <- max(testSrc$bizdate)
    tsd<- ts(testSrc$dp,start=c(2016,01,01),frequency=365)
    str(tsd)
    x <- ets(tsd)
    pred <- forecast(x,h=365)
    
    predata <- data.frame(pd = as.double(pred$mean))
    predata$Row <-as.integer(row.names(predata));
    predata$preRow <- predata$Row -1;
    predata$pt <- as.double(c(0));
    predata$stasid <- staid;
    predata$InitDate <- maxDate ;
    all_predata_time <<- rbind(all_predata_time,predata)
  }
}

  

 

参考:https://blog.csdn.net/wlt9037/article/details/74389807

技术分享图片

 

技术分享图片

 

以上是关于R执行报错:Error in `[<-.ts`(`*tmp*`,...only replacement of elements is allowed的主要内容,如果未能解决你的问题,请参考以下文章

R语言报错:Error in psi[j, t] <- which.max(preProb) : subscript out of bounds

R Error in :missing values are not allowed in subscripted assignments of data frames

R Error in :missing values are not allowed in subscripted assignments of data frames

[typescript] ERROR TS18002: The 'files' list in config file 'tsconfig.json' is emp

R 报错Error in plot.new() : figure margins too large

R基础