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
Created on 2021-06-16 by the reprex package (v1.0.0)