来自数据帧列表的时间分解摘要统计信息

问题描述 投票:0回答:1

我需要在R中的数据帧列表上计算摘要统计信息。数据帧是一个时间序列,其中日期列的格式设置为POSIXct。我需要计算整个数据帧中每个时间戳的最小均值和最大值。数据框不一定具有相同的日期时间范围,因此我无法通过行索引来计算统计信息。

下面是我拥有的数据格式的最小示例。

date0 <- as.POSIXct("2020-01-01 00:00")

mylist <- list(df1 = data.frame(date = seq.POSIXt(from=date0,
                                              by = "1 hour",
                                              length.out = 24),
                            y = rep(1,24)),
           df2 = data.frame(date = seq.POSIXt(from=date0,
                                              by = "1 hour",
                                              length.out = 48),
                            y = rep(0,48))
           )

因此,我需要作为输出的是一个具有日期,最小值,均值和最大值列的数据框。任何帮助将不胜感激

r apply posixct
1个回答
0
投票

如果一起将列表中的数据框绑定在一起,则这只是按日期的汇总。如果只希望在两个数据框中都显示日期,请在末尾添加%>% filter(n() == 2)

library(dplyr)

mylist %>%
  bind_rows %>%
  group_by(date) %>%
  summarise(
    n = n(),
    ymin = min(y),
    ymean = mean(y),
    ymax = max(y))

# # A tibble: 48 x 5
#    date                    n  ymin ymean  ymax
#    <dttm>              <int> <dbl> <dbl> <dbl>
#  1 2020-01-01 00:00:00     2     0   0.5     1
#  2 2020-01-01 01:00:00     2     0   0.5     1
#  3 2020-01-01 02:00:00     2     0   0.5     1
#  4 2020-01-01 03:00:00     2     0   0.5     1
#  5 2020-01-01 04:00:00     2     0   0.5     1
#  6 2020-01-01 05:00:00     2     0   0.5     1
#  7 2020-01-01 06:00:00     2     0   0.5     1
#  8 2020-01-01 07:00:00     2     0   0.5     1
#  9 2020-01-01 08:00:00     2     0   0.5     1
# 10 2020-01-01 09:00:00     2     0   0.5     1
# # ... with 38 more rows
© www.soinside.com 2019 - 2024. All rights reserved.