如何计算R中两个时间戳列的平均值?

时间:2019-03-13 16:24:38

标签: r datetime mean

我在R中有数据框,其中两列是日期时间(POSIX类)。我需要按每一行计算平均日期时间。

以下是一些可重现的示例:

a <- c(
 "2018-10-11 15:22:17",
 "2018-10-10 16:30:37",
 "2018-10-10 16:52:46", 
 "2018-10-10 16:58:33", 
 "2018-10-10 16:32:24")

b <- c(
  "2018-10-11 15:25:12", 
  "2018-10-10 16:30:39", 
  "2018-10-10 16:55:14", 
  "2018-10-10 16:58:53", 
  "2018-10-10 16:32:27")

a <- strptime(a, format = "%Y-%m-%d %H:%M:%S")
b <- strptime(b, format = "%Y-%m-%d %H:%M:%S")

f <- data.frame(a, b)

结果应该是这样的:

                    a                   b           time_mean
1 2018-10-11 15:22:17 2018-10-11 15:25:12 2018-10-11 15:23:44
2 2018-10-10 16:30:37 2018-10-10 16:30:39 2018-10-10 16:30:38
3 2018-10-10 16:52:46 2018-10-10 16:55:14 2018-10-10 16:54:00
4 2018-10-10 16:58:33 2018-10-10 16:58:53 2018-10-10 16:58:43
5 2018-10-10 16:32:24 2018-10-10 16:32:27 2018-10-10 16:32:25

我尝试了以下操作:

apply(f, 1, function(x) mean)
apply(f, 1, function(x) mean(c(x[1], x[2])))

2 个答案:

答案 0 :(得分:1)

使用apply

,而不是使用matrix(可以将其转换为class然后去除Map属性)。
f$time_mean <- do.call(c, Map(function(x, y) mean(c(x, y)), a, b))
f$time_mean
#[1] "2018-10-11 15:23:44 EDT" "2018-10-10 16:30:38 EDT" "2018-10-10 16:54:00 EDT" "2018-10-10 16:58:43 EDT"
#[5] "2018-10-10 16:32:25 EDT"

或者来自data.frame f

do.call(c, Map(function(x, y) mean(c(x, y)), f$a, f$b))

此外,另一种选择是使用numeric转换为?xtfrm类(也具有POSIXlt方法分派),执行rowMeans并转换为DateTime类,如@ jay.sf的帖子

as.POSIXlt(rowMeans(sapply(f, xtfrm)), origin = "1970-01-01")
#[1] "2018-10-11 15:23:44 EDT" "2018-10-10 16:30:38 EDT" "2018-10-10 16:54:00 EDT" "2018-10-10 16:58:43 EDT"
#[5] "2018-10-10 16:32:25 EDT"

答案 1 :(得分:1)

您可以使用数字进行计算。

f$time_mean <- as.POSIXct(sapply(seq(nrow(f)), function(x) 
  mean(as.numeric(f[x, ]))), origin="1970-01-01")
f
#                     a                   b           time_mean
# 1 2018-10-11 15:22:17 2018-10-11 15:25:12 2018-10-11 15:23:44
# 2 2018-10-10 16:30:37 2018-10-10 16:30:39 2018-10-10 16:30:38
# 3 2018-10-10 16:52:46 2018-10-10 16:55:14 2018-10-10 16:54:00
# 4 2018-10-10 16:58:33 2018-10-10 16:58:53 2018-10-10 16:58:43
# 5 2018-10-10 16:32:24 2018-10-10 16:32:27 2018-10-10 16:32:25