我有一个如下所示的数据集:
datetime <- seq.POSIXt(from=as.POSIXct("2017-05-09 11:45:01", tz="GMT"),
to=as.POSIXct("2017-05-09 12:45:00", tz="GMT"), by="sec")
group <- rep(1:120, each = 30)
sample.dat <- data.frame(datetime,group)
head(sample.dat)
datetime group
1 2017-05-09 11:45:01 1
2 2017-05-09 11:45:02 1
3 2017-05-09 11:45:03 1
4 2017-05-09 11:45:04 1
5 2017-05-09 11:45:05 1
6 2017-05-09 11:45:06 1
我想按组平均时间,例如到一个新的数据框,其中datetime列为平均时间,group列为组编号。期望的输出示例:
new.datetime group
1 2017-05-09 11:45:15 1
2 2017-05-09 11:45:45 2
3 2017-05-09 11:46:15 3
我尝试使用aggregate()
,但返回的datetime列采用数字格式,例如:
group date
1 1 1493984723
2 2 1493984753
3 3 1493984783
4 4 1493984813
5 5 1493984843
6 6 1493984873
那么我如何在所需的输出格式和平均值中平均时间?
答案 0 :(得分:2)
aggregate
似乎有用吗?
aggregate(sample.dat$datetime,FUN=mean,by=list(group))
# Group.1 x
# 1 1 2017-05-09 14:45:15
# 2 2 2017-05-09 14:45:45
# 3 3 2017-05-09 14:46:15
# 4 4 2017-05-09 14:46:45
# 5 5 2017-05-09 14:47:15
# 6 6 2017-05-09 14:47:45
答案 1 :(得分:1)
尝试
library(dplyr)
sample.dat %>% group_by(group) %>% summarise(mean(datetime))
# A tibble: 120 × 2
group `mean(datetime)`
<int> <dttm>
1 1 2017-05-09 11:45:15
2 2 2017-05-09 11:45:45
3 3 2017-05-09 11:46:15
4 4 2017-05-09 11:46:45
5 5 2017-05-09 11:47:15