【发布时间】:2015-10-19 05:26:42
【问题描述】:
我有一个数据文件,其中包含持续时间为 30 秒的采样读数。文件组织为:
> head(dframe)
timestamp power
1 2015-08-01 00:00:04 584.1379
2 2015-08-01 00:00:34 585.8087
3 2015-08-01 00:01:04 584.9335
4 2015-08-01 00:01:34 584.4366
5 2015-08-01 00:02:04 584.2829
现在将 30 秒的持续时间数据表示为每小时意味着我使用以下 R 命令:
df = aggregate(list(power=dframe$power),by=list(timestamp=cut(as.POSIXct(dframe$timestamp),"hour")),mean)
这非常有效。但是,实际问题是大文件(一年的数据)所花费的时间。我可以以某种方式减少转换过程所需的时间吗?换句话说,在 R 中,是否有任何其他最佳替代方案可以减少将秒数据转换为每小时平均数据的时间?
更新: 对于@akrun 和@Joshua 建议的相同问题,我使用了4 种不同的方法。对于堆栈溢出的其他用户,我提供了所有方法的用法和相应的时间
dframe<-read.csv(path,head=TRUE,sep=",")
dframe$timestamp<- as.POSIXct(dframe$timestamp)
xframe = dframe
#using aggregate
system.time(
df1<- aggregate(list(power=dframe$power),by=list(timestamp=cut(dframe$timestamp,"hour")),mean)
)
# using data.table
system.time(
dfx<-setDT(dframe)[, list(power= mean(power)) ,(timestamp= cut(timestamp, 'hour'))]
)
# using dplyr
system.time(
xframe %>% group_by(timestamp= cut(timestamp, 'hour')) %>% summarise(power=mean(power))
)
#using xts
system.time({
x <- xts(dframe$power,dframe$timestamp)
h <- period.apply(x, endpoints(x, "hours"), mean)
h <- data.frame(timestamp=trunc(index(h),'hours'), power=coredata(h))
})
在两个(一个月、三个月)不同的数据集上分别采用的时间是:对于一个月的数据集:
Method user system elapsed
Aggregate 0.137 0.005 0.142
data.table 0.031 0.001 0.032
dplyr 0.035 0.001 0.036
xts 0.053 0.000 0.053
对于三个月的数据集:
Aggregate 0.456 0.019 0.475
data.table 0.099 0.002 0.102
dplyr 0.099 0.004 0.103
xts 0.158 0.004 0.161
警告:除了 xts 之外的所有方法都将时间戳的类型从 POSIXct 更改为 Factor。这意味着您必须再次转换时间戳列的类型,这将导致更多的 CPU 周期。简而言之,如果最终您还需要 POSIXct 时间戳,那么 xts 是最好的,否则请使用 data.table。
数据集可以在link找到使用的数据集
【问题讨论】:
-
你可以试试
dplyr,library(dplyr); dframe %>% group_by(timestamp= cut(as.POSIXct(timestamp), 'hour') %>% summarise(power=mean(power)) -
我猜
cut导致了很多减速 -
@akrun。谢谢。 data.table 比我使用的所有选项花费的时间更少。请提供您的第一个(使用 data.table)评论作为答案。同时,我将在主要问题中添加统计数据。我仍然很困惑为什么它在早上给出了不好的结果!
标签: r time-series