使用dplyr每n分钟进行分组

时间:2014-12-21 23:41:46

标签: r xts dplyr

我有一个数据集,其中包含特定日期某个时间发生的10个事件,每个事件都有相应的值:

d1 <- data.frame(date = as.POSIXct(c("21/05/2010 19:59:37", "21/05/2010 08:40:30", 
                            "21/05/2010 09:21:00", "21/05/2010 22:29:50", "21/05/2010 11:27:34", 
                            "21/05/2010 18:25:14", "21/05/2010 15:16:01", "21/05/2010 09:41:53", 
                            "21/05/2010 15:01:29", "21/05/2010 09:02:06"), format ="%d/%m/%Y %H:%M:%S"),
                 value = c(11313,42423,64645,643426,1313313,1313,3535,6476,11313,9875))

我希望以标准数据框格式每3分钟汇总一次结果(从#34; 21/05/2010 00:00:00&#34;到&#34; 21/05/2010 23:57 :00&#34;,以便数据帧有480个箱,每个3分钟)

首先,我创建一个包含每个3分钟的分区的数据框:

d2 <- data.frame(date = seq(as.POSIXct("2010-05-21 00:00:00"), 
                            by="3 min", length.out=(1440/3)))

然后,我将两个数据帧合并在一起并删除NAs:

library(dplyr)
m <- merge(d1, d2, all=TRUE) %>% mutate(value = ifelse(is.na(value),0,value))

最后,我使用period.apply()包中的xts来汇总每个bin的值:

library(xts)
a <- period.apply(m$value, endpoints(m$date, "minutes", 3), sum)

有更有效的方法吗?它感觉不太理想。

更新#1

在Joshua的回答之后我调整了我的代码:

library(xts)
startpoints <- function (x, on = "months", k = 1) { 
  head(endpoints(x, on, k) + 1, -1) 
}

m <- seq(as.POSIXct("2010-05-21 00:00:00"), by="3 min", length.out=1440/3)
x <- merge(value=xts(d1$value, d1$date), xts(,m))
y <- period.apply(x, c(0,startpoints(x, "minutes", 3)), sum, na.rm=TRUE)

我不知道na.rm=TRUE可以与period.apply()一起使用,现在允许我跳过mutate(value = ifelse(is.na(value),0,value))。它向前迈进了一步,我对这里的xts方法非常满意,但我想知道是否有一个 dplyr解决方案我能做到在这种情况下使用。

更新#2

在尝试Khashaa的回答之后,我遇到了错误,因为我没有指定时区。所以我有:

> tail(d4)
               interval sumvalue
476 2010-05-21 23:45:00       NA
477 2010-05-21 23:48:00       NA
478 2010-05-21 23:51:00       NA
479 2010-05-21 23:54:00       NA
480 2010-05-21 23:57:00    11313
481 2010-05-22 02:27:00   643426
> d4[450,]
               interval sumvalue
450 2010-05-21 22:27:00       NA

现在,在Sys.setenv(TZ="UTC")之后,一切正常。

4 个答案:

答案 0 :(得分:8)

我不确定dplyr解决方案,但这是一个xts解决方案:

startpoints <- function (x, on = "months", k = 1) {
  head(endpoints(x, on, k) + 1, -1)
}
m3 <- seq(as.POSIXct("2010-05-21 00:00:00"),
  by="3 min", length.out=1440/3)
x <- merge(value=xts(d1$value, d1$date), xts(,m3))
y <- period.apply(x, c(0,startpoints(x, "minutes", 3)), sum, na.rm=TRUE)

更新:这是另一个xts解决方案,对于正确对齐聚合值更加小心。不建议先前的解决方案是错误的,但这种解决方案更容易遵循并在其他分析中重复。

m3 <- seq(as.POSIXct("2010-05-20 23:59:59.999"),
  by="3 min", length.out=1440/3)
x <- merge(value=xts(d1$value, d1$date), xts(,m3))
y <- period.apply(x, endpoints(x, "minutes", 3), sum, na.rm=TRUE)
y <- align.time(y, 60*3)

答案 1 :(得分:8)

lubridate-dplyr - esque解决方案。

library(lubridate)
library(dplyr)
d2 <- data.frame(interval = seq(ymd_hms('2010-05-21 00:00:00'), by = '3 min',length.out=(1440/3)))
d3 <- d1 %>% 
  mutate(interval = floor_date(date, unit="hour")+minutes(floor(minute(date)/3)*3)) %>% 
  group_by(interval) %>% 
  mutate(sumvalue=sum(value))  %>% 
  select(interval,sumvalue) 
d4 <- merge(d2,d3, all=TRUE) # better if left_join is used
tail(d4)
#               interval sumvalue
#475 2010-05-21 23:42:00       NA
#476 2010-05-21 23:45:00       NA
#477 2010-05-21 23:48:00       NA
#478 2010-05-21 23:51:00       NA
#479 2010-05-21 23:54:00       NA
#480 2010-05-21 23:57:00       NA
d4[450,]
#               interval sumvalue
#450 2010-05-21 22:27:00   643426

如果您愿意使用Date(我不是),您可以免除lubridate,并将最后的合并替换为left_join

答案 2 :(得分:3)

如果需要将数据分组到n分钟区,floor_date函数可以允许在函数的unit参数内指定多个单位。例如:

library(lubridate)
x <- ymd_hms("2009-08-03 12:25:59.23")
floor_date(x, unit = "3minutes")
  

&#34; 2009-08-03 12:24:00 UTC&#34;

使用您的示例:

library(lubridate)
library(tidyverse)

# make complete time sequence
d2 <- data.frame(timePeriod = seq(as.POSIXct("2010-05-21 00:00:00"), 
                        by="3 min", length.out=(1440/3)))

d1 %>%
  mutate(timePeriod = floor_date(date, "3minutes")) %>%
  group_by(timePeriod) %>%
  summarise(sum = sum(value)) %>%
  right_join(d2)

答案 3 :(得分:2)

最近,我们开发了padr软件包,它也可以以一种干净的方式解决这个问题。


library(lubridate)
library(dplyr)
library(padr)

d1 <- data.frame(date = as.POSIXct(c("21/05/2010 19:59:37", "21/05/2010 08:40:30", 
                                     "21/05/2010 09:21:00", "21/05/2010 22:29:50", "21/05/2010 11:27:34", 
                                     "21/05/2010 18:25:14", "21/05/2010 15:16:01", "21/05/2010 09:41:53", 
                                     "21/05/2010 15:01:29", "21/05/2010 09:02:06"), format ="%d/%m/%Y %H:%M:%S"),
                 value = c(11313,42423,64645,643426,1313313,1313,3535,6476,11313,9875))

res <- d1 %>% 
  as_tibble() %>%
  arrange(date) %>%

  # Thicken the results to fall in 3 minute buckets
  thicken(
    interval  = '3 min', 
    start_val = as.POSIXct('2010-05-21 00:00:00'),
    colname   = "date_pad") %>% 

  # Pad the results to fill in the rest of the 3 minute buckets
  pad(
    interval  = '3 min', 
    by        = 'date_pad', 
    start_val = as.POSIXct('2010-05-21 00:00:00'),
    end_val   = as.POSIXct('2010-05-21 23:57:00')) %>%

  select(date_pad, value)

res
#> # A tibble: 480 x 2
#>    date_pad            value
#>    <dttm>              <dbl>
#>  1 2010-05-21 00:00:00    NA
#>  2 2010-05-21 00:03:00    NA
#>  3 2010-05-21 00:06:00    NA
#>  4 2010-05-21 00:09:00    NA
#>  5 2010-05-21 00:12:00    NA
#>  6 2010-05-21 00:15:00    NA
#>  7 2010-05-21 00:18:00    NA
#>  8 2010-05-21 00:21:00    NA
#>  9 2010-05-21 00:24:00    NA
#> 10 2010-05-21 00:27:00    NA
#> # ... with 470 more rows

res[450,]
#> # A tibble: 1 x 2
#>   date_pad             value
#>   <dttm>               <dbl>
#> 1 2010-05-21 22:27:00 643426