【问题标题】:Creation of timestamps in milliseconds for df in r在 r 中为 df 创建时间戳(以毫秒为单位)
【发布时间】:2023-03-30 18:53:01
【问题描述】:

我有传感器数据,想创建缺失的时间戳并插入特征值。目前,我的测量值在它们之间具有不同的持续时间。我想有更高的金额。下面是一个简化的df

df1 = data.frame (
    value =c('5','15','10','10','5','15'),
    time = as.POSIXct(c('2018-06-03 19:40:00','2018-06-03 19:42:00','2018-06-03 19:43:00','2018-06-03 19:45:00','2018-06-03 19:46:00','2018-06-03 19:48:00')))

如果我使用 padr 包这样做,它可以工作。

df2=df1 %>% pad(start_val = df1$time[1], end_val = df1$time[6],interval = "sec")
df2$value <- na.approx(df2$value)

但我想每 0.1 或 0.2 秒创建一个时间戳。 padr 包可以处理 2 秒,但似乎不到 1 秒不起作用,因为我收到此消息

Error: The specified interval is invalid for the datetime variable.
Not all original observation are in the padding.
If you want to pad at this interval, aggregate the data first with thicken.

是否有可能以小于 1 秒的间隔创建时间戳? 我试过这个

seq.POSIXt(as.POSIXct(df1$time[1]), as.POSIXct(df1$time[6]), units = "seconds", by = .2)

但它只创建一个时间戳向量

【问题讨论】:

    标签: r dataframe time-series interpolation milliseconds


    【解决方案1】:

    如果您想使用 .1 或 .2,并保留日期结构和插值值,这可行(请注意,您可以在 seq 调用中更改 .1 或 .2)。

    library(tidyverse)
    library(padr)
    library(zoo)
    
    df1 = data.frame (
      value =c('5','15','10','10','5','15'),
      time = as.POSIXct(c('2018-06-03 19:40:00','2018-06-03 19:42:00',
                          '2018-06-03 19:43:00','2018-06-03 19:45:00',
                          '2018-06-03 19:46:00','2018-06-03 19:48:00')))
    
    # format decimal seconds so that it can be used to compare to the new date range
    df1$time <- format(df1$time, "%Y-%m-%d %H:%M:%OS2")
    
    # create the interval
    y = seq.POSIXt(as.POSIXct(df1$time[1]), as.POSIXct(df1$time[6]), units = "seconds", by = .2)
    
    # set up new data frame for original values and interpolation
    dy = data.frame(time = y,
                    value = NA) %>% 
      mutate(time = format(y, "%Y-%m-%d %H:%M:%OS2"))
    
    # obtain row numbers of those that already have values
    wh <- sapply(df1$time, function(x) which(dy$time == x))
    
    # return the original values to the dataset
    dy[wh, ]$value <- unlist(df1$value)
    
    # interpolate
    dy$value <- na.approx(dy$value)
    
    # take a look
    head(dy)
    #                     time    value
    # 1 2018-06-03 19:40:00.00 5.000000
    # 2 2018-06-03 19:40:00.20 5.016667
    # 3 2018-06-03 19:40:00.40 5.033333
    # 4 2018-06-03 19:40:00.59 5.050000
    # 5 2018-06-03 19:40:00.79 5.066667
    # 6 2018-06-03 19:40:01.00 5.083333 
    

    【讨论】:

    • 我收到这个错误信息: > # 将原始值返回到数据集 > dy[wh, ]$value
    • 你的环境中对象wh的类是什么?如果不是integer,则将代码更改为dy[unlist(wh), ] &lt;- unlist(df1$value)。 (事后看来,我应该使用vapply,而不是sapply。)
    • whinteger
    • 我有点难过!对象wh 中有什么? dy 是具有名为 value 的列的数据框吗?如果在控制台运行unlist(df1$value),输出是什么?
    • dy 是一个带有时间列和值列的数据框NAwh 是长度为 6 的列表,带有时间和整数 (0)。 unlist(df1$value) [1] "5" "15" "10" "10" "5" "15"
    【解决方案2】:

    我不熟悉padr,但最后一次调用应该会给你一个具有亚秒级差异的序列,它可能只是没有这样打印。 ?DateTimeClasses 中的亚秒级精度部分解释说:

    类 "POSIXct" 和 "POSIXlt" 能够表达 a 的分数 第二。 (两种形式之间的分数转换可能不是 准确,但精度优于微秒。)

    仅当设置了 options("digits.secs") 时才会打印小数秒: 见 strftime。

    因此,

    options("digits.secs"=TRUE)
    
    st <- Sys.time()
    st.s <- seq(st, st+1, 0.1)
    diff(st.s)
    # Time differences in secs
    #  [1] 0.099999905 0.100000143 0.099999905 0.100000143 0.099999905 0.099999905
    #  [7] 0.100000143 0.099999905 0.100000143 0.099999905
    
    st
    # [1] "2021-12-29 20:00:33.0 CET"
    dput(st)
    # structure(1640804433.02173, class = c("POSIXct", "POSIXt"))
    
    options("digits.secs"=FALSE)
    
    st
    # [1] "2021-12-29 20:00:33 CET"
    dput(st)
    # structure(1640804433.02173, class = c("POSIXct", "POSIXt"))
    

    相同的数据,只是打印得更准确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 2015-06-17
      • 2011-08-02
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2015-04-29
      相关资源
      最近更新 更多