## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(nestedscales)

## ----build--------------------------------------------------------------------
industries <- data.frame(
  section  = c("C", "C", "C", "D", "D"),
  division = c("C10", "C10", "C11", "D35", "D35"),
  class    = c("C101", "C102", "C110", "D351", "D352"),
  gva      = c(120, 80, 45, 300, 150)
)

ind <- scale_from_leaftable(
  industries,
  frames = c("section", "division", "class"),
  name   = "nace"
)
ind

## ----accessors----------------------------------------------------------------
scale_frames(ind)
scale_members(ind, "division")
scale_leaf_column(ind)
scale_weights(ind)

## ----structure----------------------------------------------------------------
scale_nests(ind, "section", "division")
scale_family(ind, "section", "division")

## ----recast-up----------------------------------------------------------------
x <- data.frame(
  class = c("C101", "C102", "C110", "D351", "D352"),
  jobs  = c(12, 8, 5, 30, 20),
  wage  = c(31, 29, 27, 45, 40)
)

recast_scale(x, ind,
  source = "class", target = "section",
  rule = c(jobs = "sum", wage = "weighted_mean")
)

## ----recast-down--------------------------------------------------------------
y <- data.frame(section = c("C", "D"), investment = c(100, 400))

recast_scale(y, ind, source = "section", target = "class", rule = "sum")

## ----time-scale---------------------------------------------------------------
grid <- expand.grid(m = 0:59, h = 0:23, d = 1:2)
clock <- data.frame(
  day    = sprintf("d%d", grid$d),
  hour   = sprintf("d%d_h%02d", grid$d, grid$h),
  minute = sprintf("d%d_h%02d_m%02d", grid$d, grid$h, grid$m)
)
time <- scale_from_leaftable(clock, frames = c("day", "hour", "minute"),
                             name = "clock")
time

## ----time-recast--------------------------------------------------------------
series <- data.frame(
  minute   = clock$minute,
  visitors = grid$h %/% 6 + 1,
  temp     = 18 + grid$h / 4
)
head(series, 3)

hourly <- recast_scale(series, time, source = "minute", target = "hour",
                       rule = c(visitors = "sum", temp = "mean"))
head(hourly, 3)

recast_scale(series, time, source = "minute", target = "day",
             rule = c(visitors = "sum", temp = "mean"))

## ----time-hours---------------------------------------------------------------
recast_scale(hourly[c("hour", "visitors")], time,
             source = "hour", target = "day", rule = "sum")

## ----join---------------------------------------------------------------------
join_scale(x, ind, frame = "class", attach = TRUE)

## ----subset-------------------------------------------------------------------
manufacturing <- filter_scale(ind, "section", "C")
scale_members(manufacturing)
scale_coverage(manufacturing)

scale_frames(prune_scale(ind, "division"))

## ----attributes---------------------------------------------------------------
places <- scale_from_leaftable(
  data.frame(
    country = c("North", "North", "North", "North", "South", "South", "South"),
    region  = c("N1", "N1", "N2", "N2", "S1", "S1", "S1"),
    city    = c("a", "b", "c", "d", "e", "f", "g")
  ),
  frames = c("country", "region", "city"), name = "places",
  attributes = data.frame(
    frame = c("region", "city"), member = c("N1", "e"),
    attribute = "hh_size", kind = "property", value = c(2.5, 2),
    unit_of_measure = "persons/household", source = "example"
  ),
  aliases = data.frame(frame = "city", member = "a", alias = "Alder",
                       source = "example")
)
scale_attributes(places, "hh_size", frame = "city")[
  c("member", "value", "unit_of_measure", "origin_frame")]

people <- data.frame(city = c("a", "b", "e"), people = c(3, 2, 6))
convert_scale(people, places, "hh_size",
              from_unit = "persons", to_unit = "household")

scale_match(places, c("Alder", "B", "Zed"))

## ----definition---------------------------------------------------------------
path <- file.path(tempdir(), "nace.json")
write_scale(ind, path)
scale_frames(read_scale(path))
scale_catalog()[c("id", "name", "frames", "n_leaves", "package")]

## ----definition-cleanup, include = FALSE--------------------------------------
unlink(path)

## ----product------------------------------------------------------------------
p <- scale_product(a = scale_example(), b = scale_example2())
product_size(p)

