Pivot between wide and long
Count the penguins per species and study year, with one column per year, then stack the year columns into one row per species and year. penguins has size measurements for 344 penguins near Palmer Station, Antarctica, from 2007 to 2009 (Gorman et al., 2014).
use Phibble\DataFrame;use function Phplyr\{count, pivot_longer, pivot_wider};use function Phplyr\starts_with;$data = 'https://raw.githubusercontent.com/phpstats' . '/datasets/main/csv';$penguins = DataFrame::fromCsv("$data/penguins.csv");$wide = $penguins |> count('species', 'year') |> pivot_wider( namesFrom: 'year', valuesFrom: 'n', namesPrefix: 'n_', );echo $wide, "\n\n";echo $wide |> pivot_longer( starts_with('n_'), namesTo: 'year', namesPrefix: 'n_', valuesTo: 'penguins',);use Phibble\DataFrame;use function Phplyr\{phplyr, starts_with};$data = 'https://raw.githubusercontent.com/phpstats' . '/datasets/main/csv';$penguins = DataFrame::fromCsv("$data/penguins.csv");$wide = phplyr($penguins) ->count('species', 'year') ->pivotWider( namesFrom: 'year', valuesFrom: 'n', namesPrefix: 'n_', ) ->get();echo $wide, "\n\n";echo phplyr($wide)->pivotLonger( starts_with('n_'), namesTo: 'year', namesPrefix: 'n_', valuesTo: 'penguins',);# A tibble: 3 × 4
species n_2007 n_2008 n_2009
<chr> <int> <int> <int>
1 Adelie 50 50 52
2 Chinstrap 26 18 24
3 Gentoo 34 46 44
# A tibble: 9 × 3
species year penguins
<chr> <chr> <int>
1 Adelie 2007 50
2 Adelie 2008 50
3 Adelie 2009 52
4 Chinstrap 2007 26
5 Chinstrap 2008 18
6 Chinstrap 2009 24
7 Gentoo 2007 34
8 Gentoo 2008 46
9 Gentoo 2009 44
count()gives one row per species and year, with the count inn.pivot_wider()makes one column for each value ofyear, adds then_prefix, and fills the columns fromn. The columns it doesn’t use, herespecies, identify the output rows.pivot_longer()stacks the columns thatstarts_with('n_')selects. Each column name, without then_prefix, goes intoyear, and each value goes intopenguins.yearholds strings, because the values come from column names.
Reference: tidyr · Selecting columns