用Y-m-d H:M:S格式求正负乘的平均值

时间:2022-05-23 21:31:58

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:

我有一个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")

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

我试着用下面的代码取平均值,但是没有成功:

tapply(data$time,mean)

tapply(数据时间,美元意味着)

1 个解决方案

#1


0  

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.

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


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

如果你不需要担心时区,你可以简单地使用

mean.POSIXct(data$time)

or even just mean(data$time).

或者只是意味着美元(数据)。

#1


0  

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.

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


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

如果你不需要担心时区,你可以简单地使用

mean.POSIXct(data$time)

or even just mean(data$time).

或者只是意味着美元(数据)。