sp_add_codelist: Add codelist data to downloaded data

View source: R/codelists.R

sp_add_codelistR Documentation

Add codelist data to downloaded data

Description

Joins a provided codelist, or downloads and processes one if necessary, and adds it to the data.

Usage

sp_add_codelist(
  data,
  codelist = NULL,
  period_column = .data$vykaz_date,
  by = NULL,
  redownload = FALSE,
  dest_dir = NULL
)

Arguments

data

a data frame returned by sp_get_table().

codelist

The codelist to add. Either a character vector of length one (see sp_tables for possible values), or a data frame returned by sp_get_codelist().

period_column

Unquoted column name of column identifying the data period in data. Leave to default if you have not changed the data object returned by sp_get_table().

by

character. Columns by which to join the codelist. Same form as for ⁠dplyr::left_join()``.⁠.

redownload

Redownload even if file has already been downloaded? Defaults to FALSE.

dest_dir

character. Directory in which downloaded files will be stored. If left unset, will use the statnipokladna.dest_dir option if the option is set, and tempdir() otherwise. Will be created if it does not exist.

Details

The data argument should be a data frame produced by sp_get_table() If this is true, the period_column argument is not needed. The codelist argument, if a data frame, should be a data frame produced by sp_get_codelist(). Specifically, it assumes it contains the following columns:

  • start_date, a date

  • end_date, a date

  • column with the code, character usually named the same as the codelist

#' You can usually tell which codelist you need from the name of the column whose code you are looking to expand, e.g. the codes in column paragraf can be expanded by codelist paragraf.

The function filters the codelist to obtain a set of entries relevant to the time period of data. If data contains tables for multiple periods, this is handled appropriately. Codelist-originating columns in the resulting data frame are renamed so they do not interfere with joining additional codelists, perhaps in a single pipe call.

Note that some codelists are "secondary" and can only be joined onto other codelists. If a codelist does not join using sp_add_codelis(), store the output of sp_get_codelist() and join it manually using dplyr.

Value

A tibble of same length as data, with added columns from codelist. See Details.

See Also

Other Core workflow: add_codelist(), get_codelist(), sp_get_codelist(), sp_get_dataset(), sp_get_table()

Examples

## Not run: 
sp_get_table("budget-central", 2017) %>%
  sp_add_codelist("polozka") %>%
  sp_add_codelist("paragraf")

pol <- sp_get_codelist("paragraf")
par <- sp_get_codelist("polozka")

sp_get_table("budget-central", 2017) %>%
  sp_add_codelist(pol) %>%
  sp_add_codelist(par)

## End(Not run)

statnipokladna documentation built on Sept. 11, 2024, 8:08 p.m.