A quick way with tidyr
's newer pivot_longer
function, which allows you to reshape data and split columns in one step. Taking a look at the column names:
names(d)
#> [1] "X2019q1" "X2019q2" "X2019q3"
You'll see that they start with X
to make the names valid, and that the year and quarter are separated by "q"
. Use that as your delimiter in pivot_longer
to split out the year and quarter, then remove the non-digit from year. Optionally, you could use dplyr::mutate
to convert columns to numeric.
library(tidyr)
d %>%
pivot_longer(everything(), names_to = c("Year", "Quarter"),
names_sep = "q", values_to = "Value") %>%
dplyr::mutate(Year = stringr::str_remove(Year, "\\D"))
#> # A tibble: 3 x 3
#> Year Quarter Value
#> <chr> <chr> <dbl>
#> 1 2019 1 1
#> 2 2019 2 2
#> 3 2019 3 3