【问题标题】:Reshaping data.frame from wide to long creating more than one column from groups of variables将 data.frame 从宽重塑为长,从变量组中创建多列
【发布时间】:2018-12-06 15:13:30
【问题描述】:

我试图根据自己的需要调整我对 Reshape() 的了解,但我做不到。 我的 data.frame 有两组列(a 和 b),我想将它们分别重塑为长格式。 它也有我想保持不变的变量。像这样:

id   2010a  2011a  2012a  char  2010b  2011b  2012b
 1     1      2      3      x     5      6      7
 2     1      2      3      y     5      6      7
 3     1      2      3      z     5      6      7
 4     1      2      3      x     5      6      7

到这种长格式

id    year     a   b  char
 1    2010     1   5   x 
 2    2010     1   5   y  
 3    2010     1   5   z  
 4    2010     1   5   x  
 1    2011     2   6   x 
 2    2011     2   6   y
 3    2011     2   6   z
 4    2011     2   6   x
 1    2012     3   7   x
 2    2012     3   7   y
 3    2012     3   7   z
 4    2012     3   7   x

谢谢!

【问题讨论】:

    标签: r dataframe reshape


    【解决方案1】:

    tidyr的解决方案:

    library(tidyr)
    library(dplyr)
    
    dt_final <- gather(dt_initial, key = year, value = value, -id) %>%
      separate(col=year, into=c("year", "name"), sep=-1) %>%
      spread(key = name, value = value) %>%
      arrange(id, year)
    

    【讨论】:

      【解决方案2】:

      这个怎么样?

      library(data.table)
      data2 <- melt(setDT(data), id.vars = "id", variable.name = "year")
      data2[, l := substr(year, 6,6)][, year := gsub("[a-zA-Z]", "", year)]
      dcast(data2, id + year ~ l, value.var = "value")[order(year, id)]
          id year a b
       1:  1 2010 1 5
       2:  2 2010 1 5
       3:  3 2010 1 5
       4:  4 2010 1 5
       5:  1 2011 2 6
       6:  2 2011 2 6
       7:  3 2011 2 6
       8:  4 2011 2 6
       9:  1 2012 3 7
      10:  2 2012 3 7
      11:  3 2012 3 7
      12:  4 2012 3 7
      

      数据:

      data <- data.frame(
        id = 1:4,
        `2010a` = c(1L, 1L, 1L, 1L),
        `2011a` = c(2L, 2L, 2L, 2L),
        `2012a` = c(3L, 3L, 3L, 3L),
        `2010b` = c(5L, 5L, 5L, 5L),
        `2011b` = c(6L, 6L, 6L, 6L),
        `2012b` = c(7L, 7L, 7L, 7L)
      )
      

      【讨论】:

        猜你喜欢
        • 2021-10-26
        相关资源
        最近更新 更多