Skip to contents

Merges annual data into the main skeleton data structure for a specific ISO year. This function is used for adding data that is measured or recorded annually, such as yearly income, employment status, or annual health assessments.

Usage

add_annual(skeleton, data, id_name, isoyear)

Arguments

skeleton

A data.table containing the main skeleton structure with id and time variables

data

A data.table containing the annual data to be merged. The function does not add or change any column of data.

id_name

Character string specifying the name of the ID variable in the data

isoyear

Integer specifying the ISO year for which the data applies

Value

The skeleton data.table is modified by reference with annual data merged in. Columns from data that already exist in skeleton will be prefixed with "i."

The function also returns the skeleton, invisibly. The return is the object the caller passed, while that object has a free column slot for every new column. Past that point data.table cannot grow the column list in place, so the new columns land on the returned table alone. A caller that passes an expression rather than a variable MUST use the return value.

Any other name that pointed at the same table before the call is stale afterwards. R cannot grow a list in place. A growth therefore leaves the caller's binding on a NEW object, and every other name on the old one. Take an alias after the call, never before it.

Duplicated IDs

An ID that has more than one row in data has no single value for the year. Such a person gets NA in every added column, on all skeleton rows of isoyear. This applies also to a column that existed before the call. The function gives one warning that states the number of duplicated IDs.

See also

create_skeleton for creating the skeleton structure, add_onetime for one-time data, make_lowercase_names for data preprocessing

Other data_integration: add_cancer_without_morphology(), add_cods(), add_diagnoses(), add_onetime(), add_operations(), add_quality_registry(), add_rx()

Examples

# Load fake data
data("fake_person_ids", package = "swereg")
data("fake_annual_family", package = "swereg")
swereg::make_lowercase_names(fake_annual_family)

# Create skeleton
skeleton <- create_skeleton(fake_person_ids[1:5], 2020, "2022-52")

# Add annual family data for 2021
add_annual(skeleton, fake_annual_family, "lopnr", 2021)

# Check data was added only for 2021
skeleton[isoyear == 2021 & is_isoyear == TRUE, .(id, isoyear, famtyp)]
#> Empty data.table (0 rows and 3 cols): id,isoyear,famtyp