简体   繁体   中英

R functions using time series and forecast functions

Here is the deal: I have a data frame with 14 columns and 20 rows. I want to apply a forecast function on each of the columns and append 11 more rows corresponding to each column. What is the best way to do so?

In other words a 20*14 data frame becomes a 31*14 data frame.

Here is the function I wish to apply:

time_series <- ts(summary, frequency=7, start=c(1,1)
for(i in 1:ncol(time_series))
{
  forecast(time_series[,i],11)
}

the original data frame is : summary

please help with this . I can manually iterate and do this but I want to do this more effectively.

You can try the usual sapply :

  new.df<-rbind(time_series, sapply(time_series, function(x){ forecast(x,11)$mean }))
  new.time_series <- ts(new.df, frequency=7, start=c(1,1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM