Skip to contents

Identifies the minimum glucose value occurring within a specified time window before a given start point. This function is useful for analyzing glucose patterns preceding specific events or time points.

Usage

find_min_before_hours(df, start_point_df, hours)

Arguments

df

A dataframe containing continuous glucose monitoring (CGM) data. Must include columns:

  • id: Subject identifier (string or factor)

  • time: Time of measurement (POSIXct)

  • gl: Glucose value (integer or numeric, mg/dL)

start_point_df

A dataframe with column start_index (R-based index into df)

hours

Number of hours to look back from the start point

Value

A list containing:

  • min_index: Tibble with R-based (1-indexed) row numbers of minimum glucose (min_index). The corresponding occurrence time is df$time[min_index] and glucose is df$gl[min_index].

  • episode_counts: Tibble with episode counts per subject (id, episode_counts)

  • episode_start: Tibble with all episode starts with columns:

    • id: Subject identifier

    • time: Timestamp at which the minimum occurs; equivalent to df$time[index]

    • gl: Glucose value at the minimum; equivalent to df$gl[index]

    • index: R-based (1-indexed) row number(s) in df denoting where the minimum occurs

Examples

# Load sample data
library(iglu)
data(example_data_5_subject)
data(example_data_hall)

# Create start points for demonstration (using row index)
start_index <- seq(1, nrow(example_data_5_subject), by = 100)
start_points <- data.frame(start_index = start_index)

# Find minimum glucose in previous 2 hours
min_before <- find_min_before_hours(example_data_5_subject, start_points, hours = 2)
print(paste("Found", length(min_before$min_index), "minimum points"))
#> [1] "Found 1 minimum points"

# Find minimum glucose in previous 1 hour
min_before_1h <- find_min_before_hours(example_data_5_subject, start_points, hours = 1)

# Analysis on larger dataset
large_start_index <- seq(1, nrow(example_data_hall), by = 200)
large_start_points <- data.frame(start_index = large_start_index)
large_min_before <- find_min_before_hours(example_data_hall, large_start_points, hours = 2)
print(paste("Found", length(large_min_before$min_index), "minimum points in larger dataset"))
#> [1] "Found 1 minimum points in larger dataset"