Home > database >  Reshape wide data to long with multiple columns in R
Reshape wide data to long with multiple columns in R

Time:08-26

I have the following data:

country <- c("afg", "alb", "aus")
gdp_2018 <- c(123, 532, 555)
gdp_2019 <- c(233, 531, 621)
gdp_2020 <- c(112, 231, 323)
inf_2018 <- c(0.1, 0.2, 0.3)
inf_2019 <- c(0.01, 0.01, 0.2)
inf_2020 <- c(0.5, 0.4, 0.4)
df <- cbind.data.frame(country, gdp_2018, gdp_2019, gdp_2020,
                       inf_2018, inf_2019, inf_2020)

I want to convert this wide data to a long format, like so:

country <- c("afg","afg","afg", "alb","alb","alb", "aus", "aus", "aus")
year <- c(2018, 2019, 2020, 2018, 2019, 2020, 2018, 2019, 2020)
gdp <- c(123, 532, 555,
  233, 531, 621,112, 231, 323)
inf <- c(0.1, 0.2, 0.3, 0.01, 0.01, 0.2, 0.5, 0.4, 0.4)

long_df <- cbind.data.frame(country, year, gdp, inf)

CodePudding user response:

Use .value in pivot_longer to pivot multiple columns. In names_to, ".value" relates to the multiple columns you want to pivot (here gdp and inf), which are followed by the year, and separated (names_sep) by a _. This all needs to be set up in the names_to and names_sep arguments.

library(tidyr)
df %>% 
  pivot_longer(-country, names_to = c(".value", "year"), names_sep = "_")

  country year    gdp   inf
  <chr>   <chr> <dbl> <dbl>
1 afg     2018    123  0.1 
2 afg     2019    233  0.01
3 afg     2020    112  0.5 
4 alb     2018    532  0.2 
5 alb     2019    531  0.01
6 alb     2020    231  0.4 
7 aus     2018    555  0.3 
8 aus     2019    621  0.2 
9 aus     2020    323  0.4 
  • Related