【问题标题】:How to extract time interval data from minute data in r如何从r中的分钟数据中提取时间间隔数据
【发布时间】:2019-05-18 22:14:25
【问题描述】:

我正在尝试从 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 分钟查找行:

【问题讨论】:

  • 您能在示例中添加更多数据吗?
  • 那么您想要Time 值在5 分钟标记处的行吗?
  • 欢迎来到 SO!你能提供想要的输出吗?
  • Select every nth row from dataframe 的可能重复项,因为您知道每一行之间的间隔为 1 百万
  • 我没有使用过查看每第 n 行,因为缺少一些数据

标签: r dplyr


【解决方案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)

【讨论】:

  • 过滤器示例会选择以 0 或 5 结尾的分钟标记吗?
  • 是的,它只是检查分钟是否是5 的整数倍,即分钟标记除以 5 余数为 0。我也添加了 substr 选项进行过滤,但它们给出相同的结果。
  • 谢谢。我已经尝试了代码并且它有效。如果开头缺少行,即如果从 14:29 开始没有 14:30,我也在寻求帮助来创建间隔
  • 您必须在问题中添加更多内容,说明在这些情况下您想要做什么,因为它不是 100% 清楚的。选择下一个?上一个?如果缺少 >1 怎么办?如果单行是最接近两个不同的 5 分钟 makrs 的非缺失时间值,应该使用两次怎么办?等等。
猜你喜欢
  • 1970-01-01
  • 2016-08-18
  • 2017-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多