【问题标题】:Plot time series knowing only start time/date and sampling periods绘制仅知道开始时间/日期和采样周期的时间序列
【发布时间】:2018-05-03 14:20:30
【问题描述】:

我想用以下数据绘制一个密度时间序列:

密度向量 (4,2,5,8,4,6,4) 采样周期向量(单位:秒)(2,2,2,2,3,2,2)

如您所见,采样周期不是恒定的。我只知道开始的日期和时间。

我不知何故需要将开始时间日期分配给第一次测量,然后为以下测量计算以下日期和时间,但我不知道如何准确编码。

【问题讨论】:

    标签: r


    【解决方案1】:

    尝试首先在 ts 中转换所需的向量,提供初始开始时间和周期的 cumsum。 我假设您对连续过程进行采样(没有跨越/死亡时间)

    require (lubridate)
    require (tidyr)
    require (ggplot2)
    require (ggfortify)
    require (timetk)
    
    
    density <- c (4,2,5,8,4,6,4)
    seconds <- c (2,2,2,2,3,2,2)
    starttime <- 0
    time <- 0 + cumsum (seconds)
    
    df <- as.data.frame (cbind (time, seconds, density))
    df$time <- as_datetime(df$time)
    df$ts <- tk_ts (df, select = density)
    
    autoplot (df$ts, ts.geom = 'bar', fill = 'blue')
    

    【讨论】:

      【解决方案2】:

      根据添加到开始的秒数的累积总和绘制密度。

      dens <- c(4,2,5,8,4,6,4)
      secs <- c(2,2,2,2,3,2,2)
      st <- as.POSIXct("2000-01-01 00:00:00")
      
      plot(st + cumsum(secs), dens, xlab = "", type = "l")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-05
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-06-20
        • 2021-05-18
        相关资源
        最近更新 更多