Skip to contents

From a pivot_table object put column(s) back as rows.

Usage

unpivot(x)

Arguments

x

A pivot_table object.

Value

a data.table.

Examples

library(flexpivot)
library(magrittr)
data("nobel_laureates")

# Revert format
nobel_laureates %>%
  pivot_table(
    rows = "category",
    cols = c("gender", "birth_continent")
  ) %>%
  unpivot()
#>                    category gender birth_continent stats  value
#>   1:              Chemistry   male          Europe     n 101.00
#>   2:              Chemistry   male          Europe     p  10.63
#>   3:              Chemistry   male          Europe p_col  21.49
#>   4:              Chemistry   male          Europe p_row  54.89
#>   5:      Economic Sciences   male          Europe     n  29.00
#>  ---                                                           
#> 892: Physiology or Medicine  Total           Total p_row 100.00
#> 893:                  Total  Total           Total     n 950.00
#> 894:                  Total  Total           Total     p 100.00
#> 895:                  Total  Total           Total p_col 100.00
#> 896:                  Total  Total           Total p_row 100.00

nobel_laureates %>%
  pivot_table(
    rows = "category",
    cols = "birth_continent"
  ) %>%
  unpivot()
#>                    category birth_continent stats  value
#>   1:              Chemistry          Europe     n 103.00
#>   2:              Chemistry          Europe     p  10.84
#>   3:              Chemistry          Europe p_col  20.85
#>   4:              Chemistry          Europe p_row  55.98
#>   5:      Economic Sciences          Europe     n  30.00
#>  ---                                                    
#> 220: Physiology or Medicine           Total p_row 100.00
#> 221:                  Total           Total     n 950.00
#> 222:                  Total           Total     p 100.00
#> 223:                  Total           Total p_col 100.00
#> 224:                  Total           Total p_row 100.00