You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按15分钟间隔规整时间序列DataFrame并取前一分钟值

按15分钟间隔规整时间序列,取目标时间戳前一分钟的数值

处理思路

要实现需求,核心是生成15分钟间隔的目标时间戳,再为每个目标时间戳匹配其前一分钟整区间对应的Heat Capacity值。具体步骤:

  • 先将原始数据按分钟聚合,保留每个分钟的最后一条记录(原始数据每10秒采集一次,取最后一条代表该分钟的有效数值)
  • 生成覆盖原始数据时间范围的15分钟间隔时间序列
  • 将目标时间戳与前一分钟的聚合数据关联,筛选有效匹配结果

代码实现

首先加载工具包并导入原始数据:

library(dplyr)
library(lubridate)

# 原始时间序列数据
df <- structure(list(Date_Time = structure(c(1685975396, 1685975406, 
1685975416, 1685975426, 1685975436, 1685975446, 1685975457, 1685975467, 
1685975477, 1685975487, 1685975497, 1685975507), tzone = "UTC", class = c("POSIXct", 
"POSIXt")), `Heat Capacity` = c(-209L, 
-209L, -203L, -202L, -197L, -196L, -191L, -185L, -185L, -184L, 
-184L, -185L)), row.names = c(NA, -12L), class = c("tbl_df", 
"tbl", "data.frame"))

执行数据规整操作:

# 1. 按分钟聚合原始数据,保留每个分钟的最后一条记录
df_minute <- df %>%
  mutate(分钟时间 = floor_date(Date_Time, "minute")) %>%
  group_by(分钟时间) %>%
  slice_tail(n = 1) %>%
  ungroup()

# 2. 生成15分钟间隔的目标规整时间戳
start_time <- floor_date(min(df$Date_Time), "15 minutes")
end_time <- ceiling_date(max(df$Date_Time), "15 minutes")
target_timestamps <- seq(start_time, end_time, by = "15 minutes")

# 3. 匹配每个规整时间戳前一分钟的数值
result_df <- tibble(规整时间戳 = target_timestamps) %>%
  mutate(匹配分钟 = floor_date(规整时间戳 - minutes(1), "minute")) %>%
  left_join(df_minute, by = c("匹配分钟" = "分钟时间")) %>%
  select(规整时间戳, `Heat Capacity`) %>%
  filter(!is.na(`Heat Capacity`))

结果示例

运行代码后得到的规整结果:

print(result_df)
#> # A tibble: 1 × 2
#>   规整时间戳          `Heat Capacity`
#>   <dttm>                        <int>
#> 1 2023-06-05 14:30:00             -209

其中2023-06-05 14:30:00对应的是14:29分钟的最后一条记录数值-209,完全符合需求中的示例逻辑。

内容的提问来源于stack exchange,提问作者Golem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:53:13