【问题标题】:Disaggregate to minutes from a specific time interval sum in R从 R 中的特定时间间隔总和分解为分钟
【发布时间】:2017-06-18 01:28:36
【问题描述】:

我需要每小时汇总一次数据,但首先要将其拆分为几分钟。我的数据如下所示:

# Data set:
dd <- read.table(header=TRUE, sep=",", text="
time, counts, counts_sec
2016-07-29 13:24:00, 10, 38
2016-07-29 13:44:00, 254, 1200
2016-07-29 14:04:00, 287, 1200
2016-07-29 14:24:00, 301, 1200")
dd$time <- as.POSIXct(dd$time)

计数表示 counts_sec 的累积总和(38 秒内计数 10 次)。通常测量周期为 20 分钟(1200 秒)。我现在想要分钟值,下一步将这些值汇总为每小时总和。

谁能帮我解决这个问题?

【问题讨论】:

  • 您想将最小值设为 [24,44,04,24...]?
  • xts 包和例如它的函数to.period 可用于此类任务。请注意,“不可能将系列从较低的周期性转换为较高的周期性 - 例如每周到每天或每天到 5 分钟柱,因为这需要魔法。” (来自?to.period)。试试dd_xts &lt;- xts(dd$counts, order.by = dd$time)dd_xts &lt;- to.hourly(dd_xts)[, 4]

标签: r interpolation aggregation


【解决方案1】:

我认为当你有 20 分钟的观点时,不可能回到一分钟的水平,因为你不知道每分钟内到底发生了什么。

但是,您可以获得一个小时的观点。我还有一些观察可以更清楚地说明这个过程是如何运作的。

library(dplyr)
library(lubridate)


dd <- read.table(header=TRUE, sep=",", text="
time, counts, counts_sec
2016-07-29 13:24:00, 10, 38
2016-07-29 13:44:00, 254, 1200
2016-07-29 14:04:00, 287, 1200
2016-07-29 14:24:00, 301, 1200
2016-07-29 14:44:00, 254, 1200
2016-07-29 15:04:00, 287, 1200
2016-07-29 15:24:00, 301, 1200")

dd$time <- as.POSIXct(dd$time)

dd

#                  time counts counts_sec
# 1 2016-07-29 13:24:00     10         38
# 2 2016-07-29 13:44:00    254       1200
# 3 2016-07-29 14:04:00    287       1200
# 4 2016-07-29 14:24:00    301       1200
# 5 2016-07-29 14:44:00    254       1200
# 6 2016-07-29 15:04:00    287       1200
# 7 2016-07-29 15:24:00    301       1200


dd %>% 
  mutate(hour_range = 1+floor(as.numeric(difftime(time, min(time), units="hours")))) %>%   # calculate in which hour range you are based on the start time point
  group_by(hour_range) %>%                        # for each hour range
  summarise(start = min(time),                    # get the start time point
            sum_counts = sum(counts))             # get the sume of counts

# # A tibble: 3 × 3
#   hour_range               start sum_counts
#        <dbl>              <dttm>      <int>
# 1          1 2016-07-29 13:24:00        551
# 2          2 2016-07-29 14:24:00        842
# 3          3 2016-07-29 15:24:00        301

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-28
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    相关资源
    最近更新 更多