在R语言中将模型输出的小时数数值列表转换为POSIXct或Date格式的技术问询
Got it, let's walk through exactly how to turn your decimal hour values into proper datetime objects that play nice with the bupar library.
First, a quick heads-up: POSIXct and Date types represent absolute datetime points, not just relative durations. Since your data tracks hours elapsed since model startup, you’ll need to define a start timestamp (the exact moment your model began running) as a reference point—this is non-negotiable for converting relative hours to absolute timestamps.
Step 1: Set your model's start time
Replace the example below with your model’s actual launch datetime (use UTC for consistency, or your preferred time zone):
model_start <- as.POSIXct("2024-05-20 08:30:00", tz = "UTC")
Step 2: Build a conversion function
This function takes your decimal hour values, converts them to seconds (since POSIXct uses seconds since the Unix epoch), then adds that duration to your start time:
decimal_hours_to_posixct <- function(elapsed_hours, start_datetime) { # Convert hours to seconds (1 hour = 3600 seconds) elapsed_seconds <- elapsed_hours * 3600 # Add elapsed time to start point to get absolute timestamp start_datetime + elapsed_seconds }
Step 3: Test with your sample values
Let’s plug in your examples to verify the conversion works as expected:
# Your raw decimal hour data elapsed_hours <- c(1.75, 25.5) # Convert to POSIXct timestamps timestamps <- decimal_hours_to_posixct(elapsed_hours, model_start) print(timestamps) # Output: # [1] "2024-05-20 10:15:00 UTC" "2024-05-21 09:00:00 UTC"
Perfect—1.75 hours becomes 1 hour 45 minutes post-startup, and 25.5 hours translates to 1 day plus 1 hour 30 minutes later.
Step 4: Convert to Date type (if needed)
If you only need the date component (no time details), wrap the POSIXct objects with as.Date():
dates <- as.Date(timestamps) print(dates) # Output: # [1] "2024-05-20" "2024-05-21"
Integrating with Bupar
Once you have your POSIXct timestamps, you can easily create an event log for bupar. Here’s a quick example:
library(bupar) # Sample event data matching your timestamps event_data <- data.frame( case_id = c("case1", "case1", "case2", "case2"), activity = c("initiate", "complete", "initiate", "complete"), timestamp = timestamps ) # Create a bupar-compatible event log event_log <- eventlog( event_data, case_id = "case_id", activity_id = "activity", timestamp = "timestamp" )
This log will work seamlessly with all of bupar’s process mining functions.
内容的提问来源于stack exchange,提问作者hillwalker101

