Changing columns with year in the name automatically

library(tidyverse)
df <- tibble(
  city = rep(1:3, 2), 
  period = c(rep(2020, 3), rep(2021, 3)), 
  gasprice = runif(6), 
  houseprice = runif(6)
)
df
#> # A tibble: 6 x 4
#>    city period gasprice houseprice
#>   <int>  <dbl>    <dbl>      <dbl>
#> 1     1   2020   0.755      0.376 
#> 2     2   2020   0.649      0.0633
#> 3     3   2020   0.337      0.839 
#> 4     1   2021   0.0358     0.589 
#> 5     2   2021   0.254      0.0479
#> 6     3   2021   0.934      0.450
df %>% pivot_wider(id_cols = city, names_from = period, values_from = c(gasprice, houseprice))
#> # A tibble: 3 x 5
#>    city gasprice_2020 gasprice_2021 houseprice_2020 houseprice_2021
#>   <int>         <dbl>         <dbl>           <dbl>           <dbl>
#> 1     1         0.755        0.0358          0.376           0.589 
#> 2     2         0.649        0.254           0.0633          0.0479
#> 3     3         0.337        0.934           0.839           0.450

Now, lets say i wanted to change the order of my columns, so I did something like this:

col_order <- c("city", "gasprice_2020",  "houseprice_2020", "gasprice_2021", "houseprice_2020") 

df <- df[, col_order]

But, the next year, the years will change from 2020 to 2021, and from 2021 to 2022. Is there a way I can change the order of the columns with the new names without doing it automatically?

Thanks in advance! :slight_smile:

Try this

col_order <- 
  expand_grid(price = c("gasprice", "houseprice"), year = unique(df$period)) %>% 
  arrange(year, price) %>% 
  apply(1, str_c, collapse = "_") %>%
  c("city", .)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.