Skip to contents

Convert a wide-form vector (e.g., indicators or multipliers) into long form, which is often more useful for printing or joining. This is a thin wrapper around tidyr::pivot_longer(), provided so you do not need to load tidyr explicitly.

Usage

vector_transpose_longer(
  data_table,
  names_to = "nace_r2",
  values_to = "value",
  key_column_name = NULL,
  .keep = FALSE
)

vector_transpose(
  data_table,
  names_to = "nace_r2",
  values_to = "value",
  key_column_name = NULL,
  .keep = FALSE
)

Arguments

data_table

A data.frame or tibble. The first column is assumed to be a key column.

names_to

Name of the new column containing previous column names. Default: "nace_r2".

values_to

Name of the new column containing the values. Default: "value".

key_column_name

Optional. New name for the first (key) column. If NULL (default), the name is not changed.

.keep

Logical. If TRUE, keep the indicator identifier column. If FALSE (default), drop it.

Value

A tibble in long format with a key column and, if requested, the indicator identifier column.

Examples

vector_transpose_longer(
  data.frame(
    indicator = "my_indicator",
    agriculture = 0.0123,
    manufacturing = 0.1436,
    trade = 0.0921
  )
)
#> # A tibble: 3 × 2
#>   nace_r2        value
#>   <chr>          <dbl>
#> 1 agriculture   0.0123
#> 2 manufacturing 0.144 
#> 3 trade         0.0921

# Keep the indicator column
vector_transpose_longer(
  data.frame(
    indicator = "my_indicator",
    agriculture = 0.0123,
    manufacturing = 0.1436
  ),
  .keep = TRUE
)
#> # A tibble: 2 × 3
#>   indicator    nace_r2        value
#>   <chr>        <chr>          <dbl>
#> 1 my_indicator agriculture   0.0123
#> 2 my_indicator manufacturing 0.144