【问题标题】:Transform row to column and Summarise将行转换为列并汇总
【发布时间】:2021-08-05 15:59:09
【问题描述】:

谁能帮我解决这个问题

我有一个数据集,见下文

Merchant value Status
paul 5 failed
royal 3 Failed
servy 56 Success
servy 33 success
paul 4 declined
royal 5 declined
royal 6 declined
royal 22 success
paul 11 Requery declined

我要做的是总结状态栏 就像使用 excel 数据透视表一样,您可以将行转换为列并进行汇总

我想在 R 中做同样的事情......我很确定必须有一种方法可以使用 dplyr 或 tidyverse 包或任何你知道可以在 R 中完成的包来完成它

总之,我希望我的结果如下表所示。

Merchant declined failed Requerydeclined Success
paul 4 5 11
royal 11 3 22
servy 89
Total 15 8 11 111

另外我是 R 的初学者

如果我能回答这个请求,我将不胜感激。

谢谢大家

【问题讨论】:

  • 请在此处粘贴dput() 您的数据
  • 同意,不过为了方便将来参考它从剪贴板读取表格df <- read.table("clipboard", sep = '\t', header = T)

标签: r dplyr data-science tidyverse janitor


【解决方案1】:

使用dcast

library(data.table)
dcast(setDT(data), Merchant ~ Status)

xtabs

xtabs(value ~ Merchant + Status, data)

【讨论】:

    【解决方案2】:

    你的数据是:

    dat <- data.frame(merchant = c("paul", "royal", "servy", "servy", "paul", "royal", "royal", "royal", "paul"),
              value = c(5, 3, 56, 33, 4, 5, 6, 22, 11),
              Status = c("failed", "failed", "success", "success", "declined", "declined", "declined", "success", "Rquery declined"))
    

    下表可以创建:

    table <- 
      dat %>% 
      group_by(merchant, Status) %>% 
      summarise(value = sum(value), .groups = "drop") %>% 
      tidyr::pivot_wider(names_from = Status, values_from = value)
    

    你得到总和:

    table %>% 
      select(!merchant) %>% 
      summarise(across(everything(), ~ sum(.x, na.rm = T)))
    

    【讨论】:

      【解决方案3】:

      为此,您需要pivot_wider

      library(tidyverse)
      
      data %>%
         pivot_wider(names_from = Status, values_from = value)
      

      【讨论】:

        【解决方案4】:

        另一种选择是使用reshape2 包中的dcast() 函数:

        library(reshape2)
        
        Merchant<-c("paul", "royal", "servy", "servy", "paul", "royal", "royal", "royal", "paul")
        Value<-c(5,3,56,33,4,5,6,22,11)
        Status<-c("failed", "failed", "Success", "Success", "declined", "declined", "declined", "Success", "Requery declined")
        
        DF<-data.frame(Merchant=Merchant, Value=Value, Status=Status)
        
        dcast(DF, Merchant~Status, fun.aggregate=sum, value.var="Value")
        

        【讨论】:

          【解决方案5】:

          tidyverse 确实可以提供帮助,即:

          library(dplyr)
          library(tidyr) #for spread
          library(stringr) #for str_to_title
          df %>%
            mutate(Status = str_to_title(Status)) %>% # make failed and Failed the same
            group_by(Merchant, Status) %>%
            summarise(value = sum(value)) %>% #summarise so each merchant and status only has one value value
            spread(key = Status, value = value, fill = 0) #the pivot section
          

          编辑 - spread 现在已经过时,所以这里是 pivot_wider 版本。

          df %>%
            mutate(Status = str_to_title(Status)) %>% # make failed and Failed the same
            group_by(Merchant, Status) %>%
            summarise(value = sum(value)) %>% #summarise so each merchant and status only has one value value
            pivot_wider(names_from = Status, values_from = value) #the pivot section
          

          【讨论】:

          • spreadpivot_wider 取代! :-)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-13
          • 1970-01-01
          • 1970-01-01
          • 2016-11-15
          • 1970-01-01
          • 2021-08-23
          相关资源
          最近更新 更多