sm_pivot_wider {samadb}R Documentation

Reshape Long API Data to Column-Based Format

Description

This function automatically reshapes long (stacked) raw data from the API (sm_data(..., wide = FALSE)) to a wide format where each variable has its own column. It can also be used as a general purpose reshaping command - with an additional capability to handle variable labels.

Usage

sm_pivot_wider(
  data,
  id_cols = intersect(.SAMADB_T, names(data)),
  names_from = "series",
  values_from = "value",
  labels_from = if (any(names(data) == "label")) "label" else NULL,
  expand.date = FALSE,
  ...
)

Arguments

data

raw data from the API: A long format data frame where all values are stacked in a value column.

id_cols

character. Temporal identifiers of the data. By default all variables in .SAMADB_T are selected.

names_from

character. The column containing the series codes. These will become the names of the new columns in the wider data format.

values_from

character. The column containing the data values.

labels_from

character. The column containing the labels describing the series.

expand.date

logical. TRUE will call sm_expand_date on the data after reshaping.

...

further arguments passed to dcast or sm_expand_date, no conflicts between these two.

Value

A data.table with the reshaped data.

See Also

sm_pivot_longer, samadb

Examples


# Return all electricity indicators from the year 2000 onwards
sm_pivot_wider(sm_data("ELECTRICITY", from = 2000, wide = FALSE))


[Package samadb version 0.2.6 Index]