如何分组时,由R中的每个n分钟(How to group time by every n minut

2019-09-30 02:32发布

我有很多的时间序列的数据帧:

1   0:03    B   1
2   0:05    A   1
3   0:05    A   1
4   0:05    B   1
5   0:10    A   1
6   0:10    B   1
7   0:14    B   1
8   0:18    A   1
9   0:20    A   1
10  0:23    B   1
11  0:30    A   1

我想组时间序列成每6分钟和计数A和B的频率:

1   0:06    A   2
2   0:06    B   2
3   0:12    A   1
4   0:12    B   1
5   0:18    A   1
6   0:24    A   1
7   0:24    B   1
8   0:18    A   1
9   0:30    A   1

同时,该类时间序列的是人品。 我该怎么办?

Answer 1:

下面是时间转换到一种方法POSIXctcut了6个分钟的间隔时间,然后count

首先,你需要指定的年,月,日,小时,分钟和数据的秒。 这将帮助它扩展到更大的数据集。

library(tidyverse)
library(lubridate)

# sample data
d <- data.frame(t = paste0("2019-06-02 ", 
                           c("0:03","0:06","0:09","0:12","0:15",
                             "0:18","0:21","0:24","0:27","0:30"), 
                           ":00"),
                g = c("A","A","B","B","B"))

d$t <- ymd_hms(d$t) # convert to POSIXct with `lubridate::ymd_hms()`

如果检查class新的日期栏,你会看到它是“POSIXct”。

> class(d$t)
[1] "POSIXct" "POSIXt" 

现在的数据是“POSIXct”,可以cut一分间隔吧! 作为新列名为我们将增加这个新的聚合因子tc

d$tc <- cut(d$t, breaks = "6 min")  
d
                     t g                  tc
1  2019-06-02 00:03:00 A 2019-06-02 00:03:00
2  2019-06-02 00:06:00 A 2019-06-02 00:03:00
3  2019-06-02 00:09:00 B 2019-06-02 00:09:00
4  2019-06-02 00:12:00 B 2019-06-02 00:09:00
5  2019-06-02 00:15:00 B 2019-06-02 00:15:00
6  2019-06-02 00:18:00 A 2019-06-02 00:15:00
7  2019-06-02 00:21:00 A 2019-06-02 00:21:00
8  2019-06-02 00:24:00 B 2019-06-02 00:21:00
9  2019-06-02 00:27:00 B 2019-06-02 00:27:00
10 2019-06-02 00:30:00 B 2019-06-02 00:27:00

现在,您可以group_by这个新的时间间隔( tc )和您的分组列( g ),并计算出现次数的频率。 获得的观测的频率的组中是一个相当普遍的操作,因此dplyr提供count为这样:

count(d, g, tc)
# A tibble: 7 x 3
  g     tc                      n
  <fct> <fct>               <int>
1 A     2019-06-02 00:03:00     2
2 A     2019-06-02 00:15:00     1
3 A     2019-06-02 00:21:00     1
4 B     2019-06-02 00:09:00     2
5 B     2019-06-02 00:15:00     1
6 B     2019-06-02 00:21:00     1
7 B     2019-06-02 00:27:00     2

如果你运行?dplyr::count()在控制台,你会看到count(d, tc)简直就是一个包装group_by(d, g, tc) %>% summarise(n = n())



Answer 2:

根据样本数据集,时间序列给出,时间的日即没有日期。

data.table封装具有ITime类,这是存储为秒在当天整数时间的天的类。 随着data.table ,我们可以使用滚动联接到次映射到6周分钟的时间间隔( 右闭区间 )的上限:

library(data.table)

# coerce from character to class ITime
setDT(ts)[, time := as.ITime(time)]

# create sequence of breaks
breaks <- as.ITime(seq(as.ITime("0:00"), as.ITime("23:59:59"), as.ITime("0:06")))

# rolling join and aggregate
ts[, CJ(breaks, group, unique = TRUE)
   ][ts, on = .(group, breaks = time), roll = -Inf, .(x.breaks, group)
     ][, .N, by = .(upper = x.breaks, group)]

返回

  upper group N 1: 00:06:00 B 2 2: 00:06:00 A 2 3: 00:12:00 A 1 4: 00:12:00 B 1 5: 00:18:00 B 1 6: 00:18:00 A 1 7: 00:24:00 A 1 8: 00:24:00 B 1 9: 00:30:00 A 1 

附录

如果该滚动的方向联接改变( roll = +Inf而不是roll = -Inf ),我们被甩闭区间

ts[, CJ(breaks, group, unique = TRUE)
   ][ts, on = .(group, breaks = time), roll = +Inf, .(x.breaks, group)
     ][, .N, by = .(lower = x.breaks, group)]

这显著改变了结果:

  lower group N 1: 00:00:00 B 2 2: 00:00:00 A 2 3: 00:06:00 A 1 4: 00:06:00 B 1 5: 00:12:00 B 1 6: 00:18:00 A 2 7: 00:18:00 B 1 8: 00:30:00 A 1 

数据

library(data.table)
ts <- fread("
1   0:03    B   1
2   0:05    A   1
3   0:05    A   1
4   0:05    B   1
5   0:10    A   1
6   0:10    B   1
7   0:14    B   1
8   0:18    A   1
9   0:20    A   1
10  0:23    B   1
11  0:30    A   1"
, header = FALSE
, col.names = c("rn", "time", "group", "value"))


文章来源: How to group time by every n minutes in R