【问题标题】:time step from 0.1 second to half hour (30 minutes) with UTC decimal hours时间步长从 0.1 秒到半小时(30 分钟),UTC 十进制小时
【发布时间】:2017-02-21 18:03:46
【问题描述】:

我的数据间隔为 0.1 秒或 10 行一秒 所以以24*60*60*10计算一天864000行。

我想通过将 0.1 秒时间步长聚合到半小时来在我的数据中找到列的平均值(风速和其他未在此处显示的变量)。所以数据将从864000行聚合到48行(一天)

输入:

           tms  Hr  Min Sec Wind speed
7/13/2014 0:00  0   0   0   3.45
7/13/2014 0:00  0   0   0.1 52.34
7/13/2014 0:00  0   0   0.2 1.23
7/13/2014 0:00  0   0   0.3 4.3
7/13/2014 0:00  0   0   0.4 1.34
7/13/2014 0:00  0   0   0.5 3.6

我想看的输出:

Year    Month   Day Hr  Wind speed
7/13/2014   7   13  0   21.92
7/13/2014   7   13  0.5 29.38
7/13/2014   7   13  1   24.18
7/13/2014   7   13  1.5 1.70
7/13/2014   7   13  2   1.80

我的每小时平均值代码,我想更改为按半小时(而不是一小时)聚合数据。其中dat 是没有tms 列的数据:所以我添加了一个日期列。

library(data.table)
library(xts)
dat <- data.table(dat)
tms <- as.POSIXct(seq(0,24*(60*60*10)-1,by=1),origin="2014-07-13",tz="UTC")
xts.ts <- data.frame(xts(dat,tms))

现在我将tms 列添加到我的数据中

Aut <- data.frame(tms,xts.ts, check.names=FALSE, row.names=NULL) 
mean2 <- aggregate(Aut, 
                   list(hour=cut(as.POSIXct(Aut$tms), "hour")),
                   mean)

但这即使是每小时也不正确。我想要半小时的数据平均值。有什么建议吗?

【问题讨论】:

  • 我不清楚您希望输出是什么。使用 xts 按时间聚合很容易,但您没有提供可重现的输入(dat 未定义)或您期望的输出示例。提供这两件事,您可能会收到几个答案。
  • 我编辑了它,但可能需要格式化
  • @JoshuaUlrich 我编辑了它并提供了输出,但不确定格式发生了什么。我想要从 0.1 秒时间步长到 30 分钟(0.5 小时)的平均值。所以将数据从 864000 行滚动到 48 行一天

标签: r data.table xts


【解决方案1】:

正如我在评论中提到的,您可以使用 xts::period.apply 轻松做到这一点:

library(xts)
options(digits.secs = 1)  # display fractional seconds
# create 1 day of timestamps that are 0.1 seconds apart
tms <- as.POSIXct(seq(0, 86400-1, by=0.1), origin="2014-07-13", tz="UTC")
# create an xts object with some random data and the times created above
set.seed(21)
xts.ts <- xts(runif(length(tms), 0, 50), tms)
# use period.apply and endpoints to calculate the 30-minute means
mean30min <- period.apply(xts.ts, endpoints(xts.ts, "mins", 30), mean)
# round up to next 30-minute period
mean30min <- align.time(mean30min, 60*30)

如果您希望结果是添加了其他列的 data.table 或 data.frame,您可以在聚合后轻松完成。

library(data.table)
dt.mean30 <- as.data.table(mean30min)
dt.mean30[, Month := .indexmon(mean30min) + 1]
dt.mean30[, Day := .indexmday(mean30min)]
dt.mean30[, Hr := .indexhour(mean30min) + .indexmin(mean30min)/60]

【讨论】:

  • @ Joshuha 谢谢。当我应用期间 .apply “端点错误(xts.ts,“min”,30):不支持的“on”参数”另外 tms 长度应该是 864000(我可以尝试修复)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-12
  • 1970-01-01
  • 2022-01-17
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多