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)