am_pivot_wider function

Reshape Long API Data to Column-Based Format

Reshape Long API Data to Column-Based Format

This function automatically reshapes long (stacked) raw data from the API (am_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.

am_pivot_wider( data, id_cols = intersect(c("ISO3", .AMT), 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 .AMT and "ISO3" 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 am_expand_date on the data after reshaping.
  • ...: further arguments passed to pivot or am_expand_date.

Returns

A data.table with the reshaped data.

Examples

# Return all indicators for Kenya and Nigeria from the year 2000 onwards am_pivot_wider(am_data(c("KEN", "NGA"), from = 2000, wide = FALSE))

See Also

am_pivot_longer, africamonitor

  • Maintainer: Sebastian Krantz
  • License: GPL-3
  • Last published: 2024-01-25