Coerce xts to ts in R

后端 未结 5 1844
Happy的楠姐
Happy的楠姐 2021-01-07 02:54

I have xts time-series object for 10 days of data. The data is sampled at minutes frequency. Therefore, for each day, I have 1440 observations. I need to coerce

5条回答
  •  时光取名叫无心
    2021-01-07 03:26

    library(xts)
    library(ggplot2)
    library(reshape2)
    
    set.seed(42)
    timevalues = "20150101 0000/20150110 2359"
    timesequence <- timeBasedSeq(timevalues)
    min_data <- xts(rnorm(14400),timesequence)
    
    ts_data <- ts(as.numeric(min_data), frequency = 1440)
    out <- stl(ts_data, s.window = "per")
    time.series <- as.data.frame(cbind(ts_data, out$time.series))
    colnames(time.series) <- c("Data", "Seasonal", "Trend", "Remainder")
    time.series$Date <- timesequence
    time.series <- melt(time.series, 'Date')
    
    ggplot(time.series, aes(x=Date, y=value)) + 
      geom_line() +
      facet_free(variable~.)
    

提交回复
热议问题