如何从R中的分钟数据中提取时间间隔数据

时间:2018-12-18 13:20:49

标签: r dplyr

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

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"))

我看过:

Create a time interval of 15 minutes from minutely data in R?

How to subset and extract time series by time interval in row

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

但是我不确定如何将其与filter结合使用。

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

Desired Output: find rows at 30 minute intervals:

1 个答案:

答案 0 :(得分:1)

您可以提取带有分钟标记的行,该行以0或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

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)