interpolate_gaps: Interpolate gaps in location tracking data (two-phase...

View source: R/interpolate_gaps.R

interpolate_gapsR Documentation

Interpolate gaps in location tracking data (two-phase approach)

Description

Phase 1: Interpolates small gaps (gap <= max_gap_small seconds) Phase 2: Interpolates larger gaps if position change is <= max_position_change meters

Usage

interpolate_gaps(
  data,
  time_col = "At",
  x_col = "X",
  y_col = "Y",
  id_col = "id_code",
  analyze_col = "Analyze",
  time_step = 1,
  max_gap_small = 10,
  max_gap_large = NULL,
  max_position_change = 0.3,
  verbose = TRUE
)

Arguments

data

A data frame with location tracking data

time_col

Name of the timestamp column (default: "At")

x_col

Name of x-coordinate column (default: "X")

y_col

Name of y-coordinate column (default: "Y")

id_col

Name of ID column (default: "id_code")

analyze_col

Name of column indicating rows to analyze (default: "Analyze")

time_step

Expected time step in seconds between consecutive observations after standardization (default: 1). Set this to match the unit used in standardize_to_seconds(), e.g. time_step = 2 if you standardized to 2-second intervals.

max_gap_small

Maximum gap size for phase 1 in seconds (default: 10)

max_gap_large

Maximum gap size for phase 2 in seconds (default: NULL for no limit)

max_position_change

Maximum position change in meters for phase 2 (default: 0.3)

verbose

Print progress messages (default: TRUE)

Details

Uses linear interpolation: X_t = X_start + (k/gap) * (X_end - X_start)

Value

Data frame with interpolated coordinates and flags:

  • imputed: 1 if row was added in phase 1 (small gaps)

  • imputed_large: 1 if row was added in phase 2 (large gaps)

  • n_entries: 0 for imputed rows

  • standardized: 0 for imputed rows

Examples

standardized_data <- data.frame(
  id_code      = c(1L, 1L),
  At           = as.POSIXct(c("2025-03-18 11:00:00", "2025-03-18 11:00:05")),
  X            = c(1.0, 1.0),
  Y            = c(2.0, 2.0),
  Analyze      = c(1L, 1L),
  n_entries    = c(1L, 1L),
  standardized = c(0L, 0L)
)
data_clean <- interpolate_gaps(standardized_data)

trackclean documentation built on July 1, 2026, 5:07 p.m.