Skip to contents

Convert a long-form vector (e.g., indicators, multipliers) into wide form, which is often more useful for binding with input–output tables. This is a thin wrapper around tidyr::pivot_wider(), provided so you do not need to load tidyr explicitly.

Usage

vector_transpose_wider(
  data_table,
  names_from,
  values_from,
  key_column_name = NULL,
  key_column_values = NULL
)

Arguments

data_table

A data.frame or tibble, normally with a key column. If the key column must be created or replaced, use key_column_name and key_column_values.

names_from, values_from

Columns specifying the names of the output columns (names_from) and the values to fill (values_from).

key_column_name

The name of the key column.

key_column_values

Optional explicit key column values. Default: NULL, in which case values are inferred from the long data.

Examples

vector_transpose_wider(
  data_table = germany_airpol[, -2],
  names_from = "induse",
  values_from = "value"
)
#> # A tibble: 9 × 9
#>   airpol CPA_A `CPA_B-E` CPA_F `CPA_G-I` `CPA_J-N` `CPA_O-T` P3_S14     P1
#>   <chr>  <int>     <int> <int>     <int>     <int>     <int>  <int>  <int>
#> 1 CO2    10448    558327 11194     71269      8792     26990 217137 904158
#> 2 CH4     1534      1160     1         4         1      1058    136   3894
#> 3 N2O       77       100     0         3         0        11     17    209
#> 4 SO2       12      1705    18        50         4        24    180   1994
#> 5 NOx       62       722    64       452        23        58    585   1967
#> 6 CO        43      1616    86       434       103       188   4198   6667
#> 7 NMVOC     20      1209    17       101        15       143    520   2024
#> 8 Dust      57       165     7        34         1         7     58    329
#> 9 Total  12252    565005 11388     72347      8939     28479 222831 921241

vector_transpose_wider(
  data_table = germany_airpol[1:8, 3:4],
  names_from = "induse",
  values_from = "value",
  key_column_values = "CO2_emission"
)
#> # A tibble: 1 × 9
#>   induse       CPA_A `CPA_B-E` CPA_F `CPA_G-I` `CPA_J-N` `CPA_O-T` P3_S14     P1
#>   <chr>        <int>     <int> <int>     <int>     <int>     <int>  <int>  <int>
#> 1 CO2_emission 10448    558327 11194     71269      8792     26990 217137 904158