关于dplyr:如何从R中的分钟数据中提取时间间隔数据

How to extract time interval data from minute data in r

我正在尝试从1分钟数据中以5分钟间隔提取行。我的数据如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
structure(list(Date = structure(c(1509408000, 1509408000, 1509408000,
1509408000, 1509408000, 1509408000), class = c("POSIXct","POSIXt"
), tzone ="UTC"), Time = structure(c(-2209021500, -2209021560,
-2209021620, -2209021680, -2209021740, -2209021800), class = c("POSIXct",
"POSIXt"), tzone ="UTC"), O = c(3674, 3675, 3674, 3675, 3675,
3675), H = c(3674, 3675, 3675, 3676, 3676, 3675), L = c(3673,
3674, 3674, 3674, 3675, 3675), C = c(3673, 3674, 3674, 3675,
3675, 3675)), row.names = c(NA, -6L), class = c("tbl_df","tbl",
"data.frame"))

structure(list(Date = structure(c(1506902400, 1506902400, 1506902400,
1506902400, 1506902400, 1506902400), class = c("POSIXct","POSIXt"
), tzone ="UTC"), Time = structure(c(-2209071300, -2209071360,
-2209071420, -2209071480, -2209071540, -2209071600), class = c("POSIXct",
"POSIXt"), tzone ="UTC"), O = c(3450, 3451, 3451, 3452, 3450,
3449), H = c(3451, 3451, 3451, 3452, 3452, 3451), L = c(3448,
3449, 3449, 3450, 3450, 3449), C = c(3448, 3451, 3450, 3451,
3452, 3450)), row.names = c(NA, -6L), class = c("tbl_df","tbl",
"data.frame"))

我看过:

从R中的分钟数据创建15分钟的时间间隔?

如何按行中的时间间隔对时间序列进行子集和提取

但没有一个完全符合我的要求。也许我可以用这个:
substr(t,15,16)=="00"

但我不确定如何将其与filter组合。

所需的输出:以30分钟的间隔查找行:

Desired


您可以使用

提取带有分钟标记的行,该行以0或5结尾。

1
2
3
4
5
df[substr(format(df$Time, '%M'), 2, 2) %in% c(0, 5),]
# or
df[as.numeric(format(df$Time, '%M')) %% 5 == 0,]
# or
df[grep('[0|5]$', format(df$Time, '%M')),]

使用filter

1
2
3
4
5
6
7
8
library(dplyr)
df %>%
  filter(substr(format(df$Time, '%M'), 2, 2) %in% c(0, 5))

# or

df %>%
  filter(as.numeric(format(df$Time, '%M')) %% 5 == 0)