我有一个大型的汽车速度数据集,高速测量一年。
该文件包含以下列:
1.TimeStamp- 31/12/13 23:48:51
2.Speed- 97.6
我想计算一天中每小时的平均速度,如:
*23:00-00:00- 110.8
*00:00-01:00- 96.17
我怎么能在R? 在R我有以下列:
$ TimeStamp : POSIXct, format: "2012-12-31 23:48:41
$ Speed : num 97.2
$ Date : Date, format: "2012-12-31"
$ Time :Class 'times' atomic [1:100000] 0.992 0.992 0.992 0.992 0.993 ...
.. ..- attr(*, "format")= chr "h:m:s"
我运行了dput(test2 [1:3,])
> dput(test2[1:3,])
structure(list(RoadId = c(12L, 12L, 12L), UnitId = c(283398L,
283398L, 283398L), TimeStamp = structure(c(1356990521, 1356990531,
1356990541), class = c("POSIXct", "POSIXt"), tzone = ""), Speed = c(97.2,
97.2, 97.2), VehicleType = c(214L, 214L, 214L)), .Names = c("RoadId",
"UnitId", "TimeStamp", "Speed", "VehicleType"), row.names = c(NA,
3L), class = "data.frame")
答案 0 :(得分:1)
可能有帮助
res <- do.call(`data.frame`, aggregate(Speed~
cbind(Hour=format(TimeStamp,'%H')), test2, mean))
res$Hour
#Hour
# 16
#Levels: 16
res$Hour <- as.numeric(as.character(res$Hour))
with(res, plot(Hour, Speed))
答案 1 :(得分:1)
您还可以使用cut
从TimeStamp
列中提取小时数。
test2$hour <- cut(test2$TimeStamp, breaks="hour")
aggregate(test2$Speed,by=list(test2$hour),FUN=mean)
# Group.1 x
#1 2013-01-01 03:00:00 97.2