将行转换为列并汇总

Transform row to column and Summarise

谁能帮我解决这个问题

我有一个数据集,见下文

商家 价值 状态
保罗 5 失败
皇家 3 失败
服务 56 成功
服务 33 成功
保罗 4 已拒绝
皇家 5 已拒绝
皇家 6 已拒绝
皇家 22 成功
保罗 11 请求被拒绝

我要做的是总结状态栏 就像使用 excel 枢轴 table 一样,您可以将行转换为列并汇总

我想在 R 中做同样的事情...我很确定一定有办法使用 dplyr 或 tidyverse 包或任何你知道的可以在 R 中做的包

总而言之,我希望我的结果如下所示 table。

商家 已拒绝 失败 拒绝重新查询 成功
保罗 4 5 11
皇家 11 3 22
服务 89
总计 15 8 11 111

另外我是 R 的初学者

如果我能得到这个请求的答复,我将不胜感激。

谢谢大家

为此你需要 pivot_wider

library(tidyverse)

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

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

另一种选择是使用 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")

您的数据是:

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 可以通过以下方式创建:

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)))

使用dcast

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

或者用xtabs

xtabs(value ~ Merchant + Status, data)