Learn R Programming

censuspyrID (version 1.0.2)

data_for_table: Prepare Population Data for Tabular Display

Description

Prepares population data for tabular display (e.g., in reports or Shiny apps). The function reshapes the data by sex, adds total population, and computes the sex ratio, while also attaching province names and labels.

Usage

data_for_table(data, reg_code, harmonized = TRUE)

Value

A data frame in wide format with columns:

  • province_id — province identifier

  • province — province name

  • year — census year

  • age5 — five-year age group label

  • Male — male population

  • Female — female population

  • Male+Female — total population

  • sex_ratio — ratio of males to females (per 100 females)

Arguments

data

A data frame containing population data for a specific province and year. Must include columns: year, province_id, sex, age5, and pop.

reg_code

Integer or character. Province code used to retrieve the province name.

harmonized

Logical. If TRUE (default), province codes are treated as harmonized; if FALSE, non-harmonized codes are used.

Details

The function performs the following steps:

  • Adds the province name using reg_name().

  • Relabels sex and age5 using reference tables in ref_label.

  • Reshapes data into wide format with separate columns for Male and Female.

  • Adds a Male+Female total population column.

  • Computes the sex ratio (Male/Female * 100).

See Also

load_pop_data(), pop_data_by_year(), get_code_label()

Examples

Run this code
if (FALSE) {
data_idn <- pop_data_by_year(load_pop_data(), 2020) |>
  pop_data_by_reg(0) #Indonesia
tab <- data_for_table(data_idn, reg_code = 0, harmonized = TRUE)
head(tab)
}

Run the code above in your browser using DataLab