简体   繁体   English

在 R 中使用多列将宽数据重塑为长数据

[英]Reshape wide data to long with multiple columns in R

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)

Use .value in pivot_longer to pivot multiple columns.pivot_longer中使用.value到 pivot 多列。 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 _ .names_to中, ".value"与您想要 pivot 的多个列(此处为gdpinf )相关,后面是year ,并用_分隔( names_sep )。 This all needs to be set up in the names_to and names_sep arguments.这一切都需要在names_tonames_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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM