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://cran.r-project.org/web/packages/tidyr/vignettes/pivot.html). First load the packages:

Longer

First inspect the data:

In tidyr, to get the longer format you need:

In tidydt, 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

## tidydt way:
fish_encounters %>% 
  wider_dt(name_to_spread = "station",value_to_spread = "seen")
#>     fish Release I80_1 Lisbon Rstr Base_TD BCE BCW BCE2 BCW2 MAE MAW
#>  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

If you want to fill with 0s, use:

tidydt currently does not support spreading multiple columns. I believe this way could keep this function simple and easy to understand. When you need this function, try spread them one by one (using a loop), or try data.table and tidyr.