Example
## # A tibble: 1 × 6
## `2018_A` `2018_B` `2019_A` `2019_B` `2020_A` `2020_B`
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 100 0 0 100 40 60
perc_tidy <- perc_wide |>
pivot_longer (
cols = everything (),
names_to = c ("year" , "type" ),
names_sep = "_" ,
values_to = "percentage"
)
perc_tidy
## # A tibble: 6 × 3
## year type percentage
## <chr> <chr> <dbl>
## 1 2018 A 100
## 2 2018 B 0
## 3 2019 A 0
## 4 2019 B 100
## 5 2020 A 40
## 6 2020 B 60
perc_tidy |>
pivot_wider (
names_from = c (year, type),
names_sep = "_" ,
values_from = percentage
)
## # A tibble: 1 × 6
## `2018_A` `2018_B` `2019_A` `2019_B` `2020_A` `2020_B`
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 100 0 0 100 40 60