When I design longer_dt and wider_dt, I could find the pivot_longer and pivot_wider in tidyr and melt and dcast in data.table. Still, designing this API is not easy, as my goal is to let users use it with least pain. Here we would try to reproduce the results in the vignette of tidyr(https://tidyr.tidyverse.org/articles/pivot.html). First load the packages:

Longer

First inspect the data:

In tidyr, to get the longer format you need:

In tidyfst, we have:

Another example from tidyr:

A warning would could come out because the merging column has different data types and do the coercion automatically.

Wider

## data
fish_encounters
#> # A tibble: 114 x 3
#>    fish  station  seen
#>    <fct> <fct>   <int>
#>  1 4842  Release     1
#>  2 4842  I80_1       1
#>  3 4842  Lisbon      1
#>  4 4842  Rstr        1
#>  5 4842  Base_TD     1
#>  6 4842  BCE         1
#>  7 4842  BCW         1
#>  8 4842  BCE2        1
#>  9 4842  BCW2        1
#> 10 4842  MAE         1
#> # ... with 104 more rows

## tidyr way:
fish_encounters %>% 
  pivot_wider(names_from = station, values_from = seen)
#> # A tibble: 19 x 12
#>    fish  Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE   MAW
#>    <fct>   <int> <int>  <int> <int>   <int> <int> <int> <int> <int> <int> <int>
#>  1 4842        1     1      1     1       1     1     1     1     1     1     1
#>  2 4843        1     1      1     1       1     1     1     1     1     1     1
#>  3 4844        1     1      1     1       1     1     1     1     1     1     1
#>  4 4845        1     1      1     1       1    NA    NA    NA    NA    NA    NA
#>  5 4847        1     1      1    NA      NA    NA    NA    NA    NA    NA    NA
#>  6 4848        1     1      1     1      NA    NA    NA    NA    NA    NA    NA
#>  7 4849        1     1     NA    NA      NA    NA    NA    NA    NA    NA    NA
#>  8 4850        1     1     NA     1       1     1     1    NA    NA    NA    NA
#>  9 4851        1     1     NA    NA      NA    NA    NA    NA    NA    NA    NA
#> 10 4854        1     1     NA    NA      NA    NA    NA    NA    NA    NA    NA
#> 11 4855        1     1      1     1       1    NA    NA    NA    NA    NA    NA
#> 12 4857        1     1      1     1       1     1     1     1     1    NA    NA
#> 13 4858        1     1      1     1       1     1     1     1     1     1     1
#> 14 4859        1     1      1     1       1    NA    NA    NA    NA    NA    NA
#> 15 4861        1     1      1     1       1     1     1     1     1     1     1
#> 16 4862        1     1      1     1       1     1     1     1     1    NA    NA
#> 17 4863        1     1     NA    NA      NA    NA    NA    NA    NA    NA    NA
#> 18 4864        1     1     NA    NA      NA    NA    NA    NA    NA    NA    NA
#> 19 4865        1     1      1    NA      NA    NA    NA    NA    NA    NA    NA

## tidyfst way:
fish_encounters %>% 
  wider_dt(name = "station",value = "seen")
#> Key: <fish>
#>       fish Release I80_1 Lisbon  Rstr Base_TD   BCE   BCW  BCE2  BCW2   MAE
#>     <fctr>   <int> <int>  <int> <int>   <int> <int> <int> <int> <int> <int>
#>  1:   4842       1     1      1     1       1     1     1     1     1     1
#>  2:   4843       1     1      1     1       1     1     1     1     1     1
#>  3:   4844       1     1      1     1       1     1     1     1     1     1
#>  4:   4845       1     1      1     1       1    NA    NA    NA    NA    NA
#>  5:   4847       1     1      1    NA      NA    NA    NA    NA    NA    NA
#>  6:   4848       1     1      1     1      NA    NA    NA    NA    NA    NA
#>  7:   4849       1     1     NA    NA      NA    NA    NA    NA    NA    NA
#>  8:   4850       1     1     NA     1       1     1     1    NA    NA    NA
#>  9:   4851       1     1     NA    NA      NA    NA    NA    NA    NA    NA
#> 10:   4854       1     1     NA    NA      NA    NA    NA    NA    NA    NA
#> 11:   4855       1     1      1     1       1    NA    NA    NA    NA    NA
#> 12:   4857       1     1      1     1       1     1     1     1     1    NA
#> 13:   4858       1     1      1     1       1     1     1     1     1     1
#> 14:   4859       1     1      1     1       1    NA    NA    NA    NA    NA
#> 15:   4861       1     1      1     1       1     1     1     1     1     1
#> 16:   4862       1     1      1     1       1     1     1     1     1    NA
#> 17:   4863       1     1     NA    NA      NA    NA    NA    NA    NA    NA
#> 18:   4864       1     1     NA    NA      NA    NA    NA    NA    NA    NA
#> 19:   4865       1     1      1    NA      NA    NA    NA    NA    NA    NA
#>       MAW
#>     <int>
#>  1:     1
#>  2:     1
#>  3:     1
#>  4:    NA
#>  5:    NA
#>  6:    NA
#>  7:    NA
#>  8:    NA
#>  9:    NA
#> 10:    NA
#> 11:    NA
#> 12:    NA
#> 13:     1
#> 14:    NA
#> 15:     1
#> 16:    NA
#> 17:    NA
#> 18:    NA
#> 19:    NA

# if no keeped groups are selected, use all except for name and value columns

If you want to fill with 0s, use:

Note that the parameter of name and value should always be provided and should be explicit called (with the parameter names attached).

More complicated example

This example comes from data.table (https://rdatatable.gitlab.io/data.table/articles/datatable-reshape.html), and has been used in tidyr too. We’ll try to do it in tidyfst in this example. If we have a data.frame as below:

And want to reshape the data.table to be like this:

The data.table::dcast and tidyr::pivot_longer could transfer it in one step, however, not so easy to understand. Here we’ll do it step by step to see what actually happens in this transfer.

In such a process, we could find that we actually get a longer table, then separate it, and wider it later. tidyfst is not going to support the complicated transfer in one step, because it might be easier to implement, but much harder to understand 3 procedures in 1 step. If you still prefer that way, use data.table::dcast and tidyr::pivot_longer instead.