Learn R Programming

sdbuildR (version 2.0.0)

saveat_func: Internal function to save data frame at specific times

Description

Internal function used to save the data frame at specific times in case save_at is not equal to dt in the simulation specifications.

Usage

saveat_func(df, time_col, new_times)

Value

Interpolated data.frame. The data frame has columns time followed by one column per variable.

Arguments

df

data.frame in wide format

time_col

Name of the time column

new_times

Vector of new times to save the data frame at

Examples

Run this code
# Recommended: Use save_at in sim_settings() to downsample simulations
sfm <- stockflow("SIR") |> sim_settings(dt = 0.01, save_at = 1)
sim <- simulate(sfm)
df <- as.data.frame(sim)
nrow(df) # Returns only times at intervals of 1
head(df)

# The saveat_func() is the underlying function used by simulate()
# Direct use is not recommended, but shown here for completeness:
sfm <- sfm |> sim_settings(save_at = 0.01)
sim <- simulate(sfm)
df <- as.data.frame(sim)
nrow(df) # Many more rows

# Manual downsampling (not recommended - use save_at instead)
new_times <- seq(min(df$time), max(df$time), by = 1)
df_wide <- as.data.frame(sim, direction = "wide")
df_manual <- saveat_func(df_wide, "time", new_times)
nrow(df_manual)

Run the code above in your browser using DataLab