I have a table looking like:
library(tidyverse)
df_initial <- tibble::tribble(
~year_month, ~n_unique, ~new_n, ~old_n,
"2021-01", 168, 159, 9,
"2021-02", 249, 244, 5,
"2021-03", 197, 186, 11
)
aka:
# A tibble: 3 × 4
year_month n_unique new_n old_n
<chr> <dbl> <dbl> <dbl>
1 2021-01 168 159 9
2 2021-02 249 244 5
3 2021-03 197 186 11
And I would like to reshape column and rows. I try via pivot_wider but I don't have any luck - especially because it doesn't find the right character view.
df_reshaped <- tibble::tribble(
~year_month, ~`2021-01`, ~`2021-02`, ~`2021-03`,
"n_unique", 168, 249, 197,
"new_n", 159, 244, 186,
"old_n", 9, 5, 11
)
Finally looking like this:
# A tibble: 3 × 4
year_month `2021-01` `2021-02` `2021-03`
<chr> <dbl> <dbl> <dbl>
1 n_unique 168 249 197
2 new_n 159 244 186
3 old_n 9 5 11
I am quite confused in how to use the function and any help would be greatly appreciated!