重塑 table:通过每年重复多列来加宽 table

Reshaping table: Widen table by repeating multiple columns per year

为了运行一个情节代码到位,我想重塑我的data.table。

我的数据 table 目前看起来与此类似:

df <- data.frame(culture=c("A","B","C","D","A","B","C","D","A","B","C","D"),
             

Year=c("2000","2000","2000","2000","2001","2001","2001","2001","2002","2002","2002","2002"),
             a=rep(1:6,2),
             b=rep(6:11,2),
             c=rep(10:15,2))

我想得到的table应该每年重复列名a、b和c。所需列:文化、a_2000、b_2000、c_2000、a_2001、b_2001、c_2001、a_2002、b_2002, c_2002

有没有简单的方法来做到这一点?

我试过:

df1<-df %>% pivot_wider(names_from = c(3:6), values_from = value)

df1<-df %>% spread(key=c(a,b,c), value, fill=NA)

df1<-df %>% nest(a,b,c, .key = 'value_col') %>% spread(key=Jahr, value=value_col) %>% unnest('2000', '2001', '2002', .sep = '_')

正如你从我失败的尝试中看到的那样,我在 r 方面还没有那么丰富的经验。

我最后一次尝试从以下建议中得到:https://community.rstudio.com/t/spread-with-multiple-value-columns/5378 然而,这也没有用

有什么建议吗?

这是一个 pivot_wider 方法:

library(tidyr)
df %>%
  pivot_wider(id_cols = culture, names_from = Year,
              values_from = !c(culture,Year))
# A tibble: 4 x 10
  culture a_2000 a_2001 a_2002 b_2000 b_2001 b_2002 c_2000 c_2001 c_2002
  <chr>    <int>  <int>  <int>  <int>  <int>  <int>  <int>  <int>  <int>
1 A            1      5      3      6     10      8     10     14     12
2 B            2      6      4      7     11      9     11     15     13
3 C            3      1      5      8      6     10     12     10     14
4 D            4      2      6      9      7     11     13     11     15

还有其他方法可以 select values_from 列。查看 help(tidyr_tidy_select)

此外,您可以控制列名的构成方式 names_glue = :

df %>%
  pivot_wider(id_cols = culture, names_from = Year,
              values_from = !c(culture,Year),
              names_glue = "Year_{Year}_{.value}")
# A tibble: 4 x 10
  culture Year_2000_a Year_2001_a Year_2002_a Year_2000_b Year_2001_b Year_2002_b Year_2000_c Year_2001_c Year_2002_c
  <chr>         <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>       <int>
1 A                 1           5           3           6          10           8          10          14          12
2 B                 2           6           4           7          11           9          11          15          13
3 C                 3           1           5           8           6          10          12          10          14
4 D                 4           2           6           9           7          11          13          11          15