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

如何在R Dataframe中提升时间序列粒度并解决‘origin’缺失报错

Hey there! Let's tackle this problem step by step. That origin error you're seeing happens because R is trying to convert a numeric value to a POSIXct datetime without knowing the starting point (the "origin" timestamp, usually 1970-01-01). This almost always comes from accidentally treating your datetime column as a numeric instead of keeping it as a proper POSIXct type.

First, let's align on a sample hourly weather dataframe that matches your description:

# Sample hourly weather data
weather_hourly <- data.frame(
  datetime = as.POSIXct(c("2024-05-20 00:00:00", "2024-05-20 01:00:00", "2024-05-20 02:00:00")),
  temperature = c(18, 17, 16),
  humidity = c(65, 68, 70)
)

Your goal is to expand this to 5-minute intervals, repeating the temperature/humidity values for every 5-minute slot within each hour. Here's how to fix the error and get your desired dataframe:

Step 1: Ensure your datetime column is a POSIXct type

First, double-check the class of your datetime column:

class(weather_hourly$datetime)

If it's not POSIXct, convert it first:

weather_hourly$datetime <- as.POSIXct(weather_hourly$datetime)

This is critical—working with proper datetime types avoids the origin error entirely.

Step 2: Expand to 5-minute intervals (using tidyverse)

We'll use dplyr and tidyr to group each hourly entry, generate the 5-minute sequence, and fill in the weather data:

library(dplyr)
library(tidyr)

weather_5min <- weather_hourly %>%
  # Group by each hourly datetime to process one hour at a time
  group_by(datetime) %>%
  # Generate a 5-minute sequence from the start of the hour to 59 minutes later (12 total intervals)
  expand(datetime = seq(datetime, datetime + 3540, by = "5 mins")) %>%
  # Join back to the original data to get temperature/humidity
  left_join(weather_hourly, by = "datetime") %>%
  # Fill down the weather values for all 5-minute slots in the hour
  fill(temperature, humidity, .direction = "down") %>%
  # Ungroup to clean up the dataframe
  ungroup()

Step 3: Alternative for large datasets (data.table)

If you're working with a huge dataset, data.table is faster and more memory-efficient:

library(data.table)

setDT(weather_hourly)
weather_5min <- weather_hourly[, .(datetime = seq(datetime, datetime + 3540, by = "5 mins")), 
                               by = .(temperature, humidity)]
# Reorder columns to match your original structure
setcolorder(weather_5min, c("datetime", "temperature", "humidity"))

Why you got the origin error

Chances are you tried converting your datetime to a numeric value (like a Unix timestamp) at some point, then tried converting back to POSIXct without specifying origin = "1970-01-01". By keeping your datetime as a POSIXct type from start to finish, seq() will generate proper datetime sequences automatically, no extra conversion needed.

Let me know if you need to adjust this for your specific dataframe structure!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:57:29