【问题标题】:unexpected result using aggregate on date_time在 date_time 上使用聚合的意外结果
【发布时间】:2021-11-08 07:35:38
【问题描述】:

我在一个大型(6 个月)数据帧中有一个 date_time POSIXct 对象,增量为 5 秒,我想将其聚合成 30 秒的“块”。 6x 5s 是 30s 所以 nrow(df)/6 给出了正确的序列长度。

我尝试了以下方法:

Date_time_30s <- aggregate(Mn$Date_time, list(seq(0, length.out = nrow(Mn)) %/% 6), FUN = mean)

前 6 个 date_times 如下所示:

"","Date_time","Depth","Temperature","Light_Level","Date"
"1",2013-10-14 12:30:00, 
"2",2013-10-14 12:30:05,
"3",2013-10-14 12:30:10,
"4",2013-10-14 12:30:15,
"5",2013-10-14 12:30:20,
"6",2013-10-14 12:30:25,

所以平均值应该是 2013-10-14 12:30:12.5,但结果是 2013-10-14 11:30:12。 没有小数秒(options(digits.secs=3) 解决了一个简单的格式问题)但是小时是错误的。

怎么了?


输入(头部(Mn))

structure(list(Date_time = structure(c(1381721400, 1381721405, 
1381721410, 1381721415, 1381721420, 1381721425), class = c("POSIXct", 
"POSIXt"), tzone = "Asia/Tokyo"), Depth = c(64.4476273148148, 
65.9476334145628, 65.9476395143109, 66.4476456140589, 67.9476517138069, 
66.9476578135549), Temperature = c(27.549999, 27.5, 27.400002, 
27.35, 27.25, 27.200001), Light_Level = c(148L, 148L, 148L, 148L, 
147L, 147L), Date = structure(c(15992, 15992, 15992, 15992, 15992, 
15992), class = "Date"), vv = c(0, 0.300001, 1e-06, 0.100001, 
0.300001, -0.199999), vv_abs = c(0, 0.300001, 1e-06, 0.100001, 
0.300001, 0.199999)), row.names = c(NA, 6L), class = "data.frame")

【问题讨论】:

    标签: r datetime aggregate


    【解决方案1】:

    在代码之前运行:

    options(digits.secs=3)
    

    你能运行这个命令吗?它将为您提供不同时区的结果。东京应该是正确的时间。

    library(lubridate)
    library(dplyr)
    Date_time_30s <- aggregate(Mn$Date_time, list(seq(0, length.out = nrow(Mn)) %/% 6), FUN = mean) %>% 
      mutate(Tokyo = with_tz(x, tzone = "Asia/Tokyo"),
             GMT = with_tz(x, tzone = "GMT"))
    

    【讨论】:

    • 这很好,但不能解决小时错误的实际问题。
    • 有趣。如果我运行您的代码,日期是正确的:"2013-10-14 12:30:12.5 JST"。所以,它必须是带有时区的东西。
    • 我更新了答案。现在可以查了吗?
    • 嘿 Bloxx,谢谢。我以为它与时区有关,但现在我意识到它是在我的本地时区 (+8) 中计算的,并且数据是针对 (+9) 的。
    • 我试过 Date_time_30s
    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 2023-03-29
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多