% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/ild_align.R
\name{ild_align}
\alias{ild_align}
\title{Align a secondary stream to primary ILD within a time window}
\usage{
ild_align(
  primary,
  secondary,
  value_var,
  window,
  time_secondary = "time",
  fun = c("mean", "median", "closest")
)
}
\arguments{
\item{primary}{An ILD object (see [is_ild()]); the stream to keep as rows.}

\item{secondary}{A data frame with id and time columns and the value variable(s) to align.}

\item{value_var}{Character. Name of the column in \code{secondary} to align (e.g. \code{"heart_rate"}).}

\item{window}{Numeric or lubridate duration. Time window (same units as \code{.ild_time_num},
e.g. seconds for POSIXct). Only secondary observations with time in
\code{(primary_time - window, primary_time]} are used.}

\item{time_secondary}{Character. Name of the time column in \code{secondary} (default \code{"time"}).}

\item{fun}{Character. Aggregation for values in window: \code{"mean"}, \code{"median"}, or \code{"closest"} (most recent in window).}
}
\value{
The primary data with a new column \code{<value_var>_aligned} (numeric; NA where no secondary obs in window).
}
\description{
For each row in the primary ILD, finds observations in the secondary
data set (same id, time within \code{window} before the primary time)
and attaches an aggregated value (e.g. mean, median, or closest).
Use when combining self-report with wearables or other streams that
have different timestamps.
}
\examples{
prim <- ild_prepare(
  data.frame(
    id = rep(1:2, each = 3),
    time = as.POSIXct(rep(c(0, 3600, 7200), 2), origin = "1970-01-01"),
    y = rnorm(6)
  ),
  id = "id", time = "time"
)
sec <- data.frame(
  id = rep(1:2, each = 4),
  time = as.POSIXct(rep(c(0, 1800, 3600, 5400), 2), origin = "1970-01-01"),
  heart_rate = 60 + rnorm(8, 0, 5)
)
ild_align(prim, sec, "heart_rate", window = 3600, fun = "mean")
}
