taking the average of POSIX times in Y-m-d H:M:S format

I have a list of times in POSIXct format ("%Y-%m-%d %H:%M:%S", tz = "EST") that I would like to take the average of:

> 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")

I have tried to take the average with the following code but this did not work:

tapply(data$time,mean)

Answers


I suspect that this gives the result that you are looking for. It provides the average time at which the observations in the list were recorded:

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

The idea is to take the average of the timestamps, by using as.numeric() to convert the POSIX values (back) into timestamps. The numerical average of these timestamps can be computed by simply using mean(). After that, the average timestamp is converted back into a time/date format with as.POSIXct(). The result depends on the choice of the timezone.


If you don't have to worry about time zones, you may simply use

mean.POSIXct(data$time)

or even just mean(data$time).


Need Your Help

Update object on masterpage through ajax webmethod

c# jquery ajax

How can i update a repeater on my masterpage, through an ajax webmethod?

TIme based protection with Safenet Sentinel LDK

hardware protection cracking dongle

I'm using Safenet Sentinel LDK to protect our commercial product.