如何对包含多个name_from列和多个values_from列的 Dataframe 执行pivot_wider?

icomxhvb  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(94)

我有一个 Dataframe 如下:

df <- data.frame(code=c("A1","A2","A3","A4","B1","B2","B3","B4"),
                pcode = c("el","el","ng","el","ng","el","el","el"),
                psaved=c(10,10,10,10,10,10,10,10),
                pcost=c(20,20,30,25,34,25,30,20),
                scode = c("e1","e2","e2","e2","e2","e2","e3","e3"),
                ssaved=c(10,10,10,10,10,10,10,10),
                scost=c(20,20,30,25,34,25,30,20),
                tcode = c("e2","e3","e1","e1","e3","e3","e2","e1"),
                tsaved=c(10,10,10,10,10,10,10,10),
                tcost=c(20,20,30,25,34,25,30,20))

我希望扩展 Dataframe ,使pcode、scode和tcode列中的内容成为单独的列,它们对应的saved和cost值位于各自的单元格中,结果应该如下所示:

我尝试执行pivot_wideer来帮助我获得更宽的 Dataframe ,其中包含pcode、scode、tcode中的变量,以便与它们各自的成本和保存值保持一致

4sup72z8

4sup72z81#

一种选择是分两步完成,首先使用pivot_longer,然后执行pivot_wider

library(tidyr)
library(dplyr)

df %>%
  rename(code1 = code) %>%
  pivot_longer(-code1,
    names_to = c("name", ".value"),
    names_pattern = "^(.)(.*)$"
  ) %>%
  select(-name) %>%
  pivot_wider(
    names_from = code,
    values_from = c(saved, cost),
    names_glue = c("{code}_{.value}")
  ) %>%
  rename(code = code1)
#> # A tibble: 8 × 11
#>   code  el_saved e1_sa…¹ e2_sa…² e3_sa…³ ng_sa…⁴ el_cost e1_cost e2_cost e3_cost
#>   <chr>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
#> 1 A1          10      10      10      NA      NA      20      20      20      NA
#> 2 A2          10      NA      10      10      NA      20      NA      20      20
#> 3 A3          NA      10      10      NA      10      NA      30      30      NA
#> 4 A4          10      10      10      NA      NA      25      25      25      NA
#> 5 B1          NA      NA      10      10      10      NA      NA      34      34
#> 6 B2          10      NA      10      10      NA      25      NA      25      25
#> 7 B3          10      NA      10      10      NA      30      NA      30      30
#> 8 B4          10      10      NA      10      NA      20      20      NA      20
#> # … with 1 more variable: ng_cost <dbl>, and abbreviated variable names
#> #   ¹​e1_saved, ²​e2_saved, ³​e3_saved, ⁴​ng_saved

相关问题