balance_panel: Balance panel data

View source: R/balance_panel.R

balance_panelR Documentation

Balance panel data

Description

Transforms an unbalanced panel dataset into a balanced one by either keeping only complete units or filling missing time points with a specified value.

Usage

balance_panel(data, unit_col = NULL, time_col = NULL,
              fill = NA_real_, method = c("fill", "complete"),
              verbose = FALSE)

Arguments

data

A data frame containing panel data.

unit_col

Column index or name identifying the individual unit (e.g., station ID, subject ID). If NULL, the first column is assumed.

time_col

Column index or name identifying the time point. If NULL, the first column matching date is used.

fill

Value used to fill missing combinations when method = "fill".

method

Balancing method: "fill" adds rows for missing unit-time combinations and fills numeric non-key columns with fill; non-numeric columns of the added rows are left as NA. "complete" keeps only units that have observations at every time point.

verbose

Logical; if TRUE, prints progress message.

Details

The function assumes that the time column is sorted and contains no duplicates within a unit. If duplicates exist, they should be handled beforehand.

Value

A balanced data frame.

Examples

balance_panel(data[1:200, c(1, 4, 17:19)], unit_col = 2, time_col = 1, method = "complete")

dataprep documentation built on Oct. 1, 2026, 5:07 p.m.