agg_regions: Aggregate to other region level

View source: R/post_process.R

agg_regionsR Documentation

Aggregate to other region level

Description

Aggregate to other region level

Add regional aggragation

Usage

agg_regions(
  x,
  from = "kunta",
  to = "maakunta",
  value_cols = c("values"),
  pass_region_codes = NULL,
  na.rm = FALSE,
  all_to_regions = TRUE,
  custom_key = NULL,
  by_name = FALSE
)

add_regional_agg(x, from = "kunta", to = "maakunta")

Arguments

x

A data.frame like object.

from

A name of reginal classification.

to

A name of reginal classification.

value_cols

A character vector of name of columns to aggregate.

pass_region_codes

Region codes to be passed true. For example whole country "SSS". Does not affect aggregation. Default NULL.

na.rm

logical. Should missing values (including NaN) be removed?

all_to_regions

logical. Should all to regions included even if not in data.

custom_key

A data.frame. Custom classification key in same form as as key from get_regionkey.

by_name

A locigal to join by name or code (default).

Functions

  • add_regional_agg():

Examples

x <- data.frame(alue_code = c("SSS", "KU049", "KU091", "KU109"),
                alue_name = c("KOKO MAA", "Espoo", "Helsinki", "Hämeenlinna"), values = c(1,1,1,2))
agg_regions(x, na.rm = TRUE)
agg_regions(x, na.rm = TRUE, pass_region_codes = "SSS")
agg_regions(x, na.rm = TRUE, pass_region_codes = "SSS", all_to_regions = FALSE)
z <- dplyr::mutate(x, values2 = c(3,4,5,6))
agg_regions(z, na.rm = TRUE, value_cols = c("values", "values2"))
agg_regions(x, na.rm = TRUE,
            custom_key = data.frame(
               kunta_code = c("SSS", "KU049", "KU091", "KU109"),
               kunta_name = c("KOKO MAA", "Espoo", "Helsinki", "Hämeenlinna"),
               maakunta_code = as.factor(c("SSS", "MK1", "MK1", "MK2")),
               maakunta_name = as.factor(c("SSS", "Maakunta1", "Maakunta1", "Maakunta2")))
               )
agg_regions(x, na.rm = TRUE,
            custom_key = data.frame(
               kunta_code = c("SSS", "KU049", "KU091", "KU109"),
               kunta_name = c("KOKO MAA", "Espoo", "Helsinki", "Hämeenlinna"),
               maakunta_code = as.factor(c("SSS", "MK1", "MK1", "MK2")),
               maakunta_name = as.factor(c("SSS", "Maakunta1", "Maakunta1", "Maakunta2"))),
               by_name = TRUE
               )

pttry/pttdatahaku documentation built on Jan. 25, 2025, 10:37 a.m.