Learn R Programming

panelr (version 1.0.0)

balance_panel: Balance panel data by filling gaps

Description

This function makes implicit missing values explicit by adding rows with NA values for entity-wave combinations that are not present in the data.

Usage

balance_panel(data, ...)

Value

A panel_data frame with all entity-wave combinations present.

Arguments

data

A panel_data frame.

...

Optional fill values specified as column = value. Any columns not specified will be filled with NA.

Details

Panel data often has implicit gaps where certain entities are not observed in certain waves. This function makes these gaps explicit by adding rows filled with NA values (or custom values if specified).

This is the inverse operation of removing incomplete cases. It can be useful for:

  • Visualizing the pattern of missing data

  • Using functions that require complete (balanced) panels

  • Explicit handling of missing waves in models

See Also

has_gaps(), scan_gaps(), complete_data()

Examples

Run this code
data("WageData")
wages <- panel_data(WageData, id = id, wave = t)

# Create data with gaps
wages_gaps <- wages[!(wages$t == 3 & wages$id == wages$id[1]), ]
nrow(wages_gaps)  # Missing one row

# Balance the panel (add NA row)
wages_balanced <- balance_panel(wages_gaps)
nrow(wages_balanced)  # Back to full size

# Balance with custom fill values
wages_balanced <- balance_panel(wages_gaps, wks = 0, union = 0)

Run the code above in your browser using DataLab