以Y-m-d H:M:S格式取POSIX时间的平均值

时间:2016-03-10 09:09:17

标签: r time posix

我有POSIXct格式的时间列表("%Y-%m-%d%H:%M:%S",tz =" EST") 我想取平均值:

> data
                  time
1  2015-10-08 17:20:17
2  2015-10-08 17:16:29
3  2015-10-08 17:28:34
4  2015-10-08 17:20:54
5  2015-10-08 17:47:37
6  2015-10-08 16:44:18
7  2015-10-08 17:36:42
8  2015-10-08 17:44:00
9  2015-10-08 17:54:36
10 2015-10-08 17:45:09
11 2015-10-08 17:50:31
12 2015-10-08 17:31:18
13 2015-10-08 17:11:28
14 2015-10-08 16:37:40
15 2015-10-08 17:57:28
16 2015-10-08 16:54:24
17 2015-10-08 17:11:46
18 2015-10-08 16:55:32
19 2015-10-08 17:31:29
20 2015-10-08 17:44:44
21 2015-10-08 16:51:12
22 2015-10-08 16:41:36
23 2015-10-08 17:20:50
24 2015-10-08 17:30:37
25 2015-10-08 12:32:14
26 2015-10-08 17:30:27
27 2015-10-08 17:21:11
28 2015-10-08 17:23:58
29 2015-10-08 17:10:19
30 2015-10-08 16:58:50
31 2015-10-08 17:01:13
32 2015-10-08 16:58:25
33 2015-10-08 17:33:15
34 2015-10-08 16:35:51
35 2015-10-08 17:48:00
36 2015-10-08 17:03:43
37 2015-10-08 17:09:46
38 2015-10-08 17:14:50
39 2015-10-08 17:26:35
40 2015-10-08 17:27:33
41 2015-10-08 17:45:03
42 2015-10-08 17:20:20
43 2015-10-08 17:32:20
44 2015-10-08 16:32:56
45 2015-10-08 17:05:29
46 2015-10-08 17:01:46
> dput(data)
structure(list(time = structure(c(1444342817.5, 1444342589, 1444343314, 
1444342854, 1444344457.5, 1444340658, 1444343802, 1444344240, 
1444344876, 1444344309, 1444344631.5, 1444343478.5, 1444342288.5, 
1444340260, 1444345048.5, 1444341264.5, 1444342306, 1444341332, 
1444343489, 1444344284, 1444341072, 1444340496.5, 1444342850.5, 
1444343437, 1444325534, 1444343427, 1444342871, 1444343038, 1444342219, 
1444341530, 1444341673, 1444341505, 1444343595, 1444340151, 1444344480.5, 
1444341823.5, 1444342186, 1444342490.5, 1444343195.5, 1444343253.5, 
1444344303, 1444342820.5, 1444343540, 1444339976.5, 1444341929.5, 
1444341706.5), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = "time", row.names = c(NA, 
-46L), class = "data.frame")

我尝试使用以下代码取平均值,但这不起作用:

tapply(data$time,mean)

1 个答案:

答案 0 :(得分:0)

我怀疑这会给你正在寻找的结果。它提供了记录列表中观察结果的平均时间:

as.POSIXct(mean(as.numeric(data$time)), origin='1970-01-01',tz="EST")
#[1] "2015-10-08 17:12:14 EST"

我们的想法是通过使用as.numeric()POSIX值(返回)转换为时间戳来获取时间戳的平均值。可以通过简单地使用mean()来计算这些时间戳的数字平均值。之后,平均时间戳将转换回as.POSIXct()的时间/日期格式。结果取决于时区的选择。

如果您不必担心时区,可以直接使用

mean.POSIXct(data$time)

甚至只是mean(data$time)