从 0.1 秒到半小时(30 分钟)的时间步长,UTC 十进制小时

time step from 0.1 second to half hour (30 minutes) with UTC decimal hours

我有数据间隔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::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]