R语言学习-问题解决-Error in `[

在输出时间序列结果的时候,出现以下问题:

for (n in 0:99)
  {
    salesTS <-ts(src_dat$rate_m[(1+24*n):(24*n+24)],frequency=12,start=c(2015,1,1)) 
    salesForecasts<- HoltWinters(salesTS)
    salesForecast2<- forecast:::forecast.HoltWinters(salesForecasts,h=12)
    output <- rbind(output,data.frame(prd_code=src_dat$prod_no[(1+24*n)],year="2017",month=seq(1:12),rate=salesForecast2$mean)  )
  }
 Error in `[<-.ts`(`*tmp*`, ri, value = c(2.7110254334953, 2.95418132827278,  : 
  only replacement of elements is allowed 
提示rbind的时候出错,分别查看rbind两端的数据

> output
      prd_code year month     rate
1  13213798874 2017     1 2.711025
2  13213798874 2017     2 2.954181
3  13213798874 2017     3 2.561491
4  13213798874 2017     4 2.313876
5  13213798874 2017     5 1.948865
6  13213798874 2017     6 1.329617
7  13213798874 2017     7 2.550668
8  13213798874 2017     8 2.089158
9  13213798874 2017     9 1.909078
10 13213798874 2017    10 2.006295
11 13213798874 2017    11 1.941801
12 13213798874 2017    12 1.855000
> tmp<-data.frame(prd_code=src_dat$prod_no[(1+24*n)],year="2017",month=seq(1:12),rate=salesForecast2$mean)
> tmp
      prd_code year month     rate
1  13213818469 2017     1 2.711025
2  13213818469 2017     2 2.954181
3  13213818469 2017     3 2.561491
4  13213818469 2017     4 2.313876
5  13213818469 2017     5 1.948865
6  13213818469 2017     6 1.329617
7  13213818469 2017     7 2.550668
8  13213818469 2017     8 2.089158
9  13213818469 2017     9 1.909078
10 13213818469 2017    10 2.006295
11 13213818469 2017    11 1.941801
12 13213818469 2017    12 1.855000
显示并没有什么问题,那么分析下各个列的属性

> class(output)
[1] "data.frame"
> str(output)
'data.frame':	12 obs. of  4 variables:
 $ prd_code: num  1.32e+10 1.32e+10 1.32e+10 1.32e+10 1.32e+10 ...
 $ year    : Factor w/ 1 level "2017": 1 1 1 1 1 1 1 1 1 1 ...
 $ month   : int  1 2 3 4 5 6 7 8 9 10 ...
 $ rate    : Time-Series  from 2017 to 2018: 2.71 2.95 2.56 2.31 1.95 ...
> class(tmp)
[1] "data.frame"
> str(tmp)
'data.frame':	12 obs. of  4 variables:
 $ prd_code: num  1.32e+10 1.32e+10 1.32e+10 1.32e+10 1.32e+10 ...
 $ year    : Factor w/ 1 level "2017": 1 1 1 1 1 1 1 1 1 1 ...
 $ month   : int  1 2 3 4 5 6 7 8 9 10 ...
 $ rate    : Time-Series  from 2017 to 2018: 2.71 2.95 2.56 2.31 1.95 ...

这个类型前所未见,查后得知是时间序列类型,附带时间属性。现在我并不需要它的时间,因此用as.numeric()做一个强制转换:

output <- rbind(output,data.frame(prd_code=src_dat$prod_no[(1+24*n)],year="2017",month=seq(1:12),rate=as.numeric(salesForecast2$mean)) )
问题解决。






你可能感兴趣的:(R语言,问题解决,碎片)