【问题标题】:convert long to wide format with two factors in R [duplicate]在R中使用两个因子将长格式转换为宽格式[重复]
【发布时间】:2016-03-28 23:24:44
【问题描述】:

我有以下数据集:

sample.data <- data.frame(Step = c(1,2,3,4,1,2,1,2,3,1,1),
                          Case = c(1,1,1,1,2,2,3,3,3,4,5),
                          Decision = c("Referred","Referred","Referred","Approved","Referred","Declined","Referred","Referred","Declined","Approved","Declined"),
                          Reason = c("Docs","Slip","Docs","","Docs","","Slip","Docs","","",""))

sample.data

      Step Case Decision Reason
1     1    1    Referred Docs
2     2    1    Referred Slip
3     3    1    Referred Docs
4     4    1    Approved
5     1    2    Referred Docs
6     2    2    Declined
7     1    3    Referred Slip
8     2    3    Referred Docs
9     3    3    Declined
10    1    4    Approved
11    1    5    Declined

是否可以在 R 中将其转换为宽表格式,在标题上做出决定,每个单元格的值是出现次数,例如:

Case    Referred    Approved    Declined    Docs     Slip
 1          3           1           0        2        0
 2          1           0           1        1        0
 3          2           0           1        1        1
 4          0           1           0        0        0
 5          0           0           1        0        0

【问题讨论】:

    标签: r reshape


    【解决方案1】:

    我们可以从tidyr使用gather/spread

     library(tidyr)
     library(dplyr)
     gather(sample.data, Var, Val, 3:4) %>%
               group_by(Case, Val) %>% 
               summarise(n=n()) %>%
               filter(Val!='') %>% 
               spread(Val, n, fill=0)
    
    #   Case Approved Declined  Docs Referred  Slip
    #   (dbl)    (dbl)    (dbl) (dbl)    (dbl) (dbl)
    #1     1        1        0     2        3     1
    #2     2        0        1     1        1     0
    #3     3        0        1     1        2     1
    #4     4        1        0     0        0     0
    #5     5        0        1     0        0     0
    

    【讨论】:

      【解决方案2】:

      使用:

      library(reshape2)
      tmp <- melt(sample.data, id.var=c("Step", "Case"))
      tmp <- tmp[tmp$value!="",]
      
      dcast(tmp, Case ~ value, value.var="Case", length)
      

      你得到:

         Case Approved Declined Docs Referred Slip
      1:    1        1        0    2        3    1
      2:    2        0        1    1        1    0
      3:    3        0        1    1        2    1
      4:    4        1        0    0        0    0
      5:    5        0        1    0        0    0
      

      使用 data.table 包,您可以使用与 reshape2 相同的 meltdcast 功能,但您不需要临时数据框:

      library(data.table)
      dcast(melt(setDT(sample.data), id.var=c("Step", "Case"))[value!=""],
            Case ~ value, value.var="Case", length)
      

      这会给你同样的结果。

      【讨论】:

        【解决方案3】:
        library(reshape2)
        
        df1 <- dcast(sample.data, Case~Decision+Reason)
        names(df1)[2:5] <- c("Approved", "Declined", "Docs", "Slip")
        df1$Referred <- df1$Docs + df1$Slip
        
        df1
        #    Case Approved Declined Docs Slip Referred
        # 1:    1        1        0    2    1        3
        # 2:    2        0        1    1    0        1
        # 3:    3        0        1    1    1        2
        # 4:    4        1        0    0    0        0
        # 5:    5        0        1    0    0        0
        

        【讨论】:

        • 正如我在这里看到的大多数长到宽格式的问题,这不会产生我正在寻找的结果。 dcast 函数给出了两个因素组合的结果,我的输出使它们保持分离。
        • 是的。你说得对。也许您需要使用dcast 的结果。也许您显示的所需输出不正确。请检查。
        • 善用dcast,加一!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        • 2015-07-18
        • 2023-02-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多