r语言 - 如何将观察值转化为变量并总结每个变量条件下的每个值



我有一个有diff列/观察值的数据集

参见下面的dataset

66768789

您可以使用pivot_wider代替tabyl,然后使用janitor代码。

library(tidyr)
library(janitor)
report_21st %>%
pivot_wider(names_from = Status, values_from = Face.value, values_fill = 0) %>%
adorn_totals("row")%>%
adorn_percentages("row")%>%
adorn_pct_formatting()%>%
adorn_ns("front")
# merchant   processing      success Transaction declined      pending     Success
#        a  10 (100.0%)   0   (0.0%)           0   (0.0%)   0   (0.0%)  0   (0.0%)
#        b   5 (100.0%)   0   (0.0%)           0   (0.0%)   0   (0.0%)  0   (0.0%)
#        c   0   (0.0%)  40 (100.0%)           0   (0.0%)   0   (0.0%)  0   (0.0%)
#        d   0   (0.0%)   0   (0.0%)          30 (100.0%)   0   (0.0%)  0   (0.0%)
#        e   0   (0.0%)  32 (100.0%)           0   (0.0%)   0   (0.0%)  0   (0.0%)
#        f   0   (0.0%)   0   (0.0%)           0   (0.0%)  21 (100.0%)  0   (0.0%)
#        g   0   (0.0%)   0   (0.0%)          23 (100.0%)   0   (0.0%)  0   (0.0%)
#        h   0   (0.0%)   0   (0.0%)           0   (0.0%)   0   (0.0%) 45 (100.0%)
#        i   0   (0.0%)   0   (0.0%)          66 (100.0%)   0   (0.0%)  0   (0.0%)
#        j   0   (0.0%)  76 (100.0%)           0   (0.0%)   0   (0.0%)  0   (0.0%)
#        k   0   (0.0%)   0   (0.0%)           0   (0.0%)  87 (100.0%)  0   (0.0%)
#        l  89 (100.0%)   0   (0.0%)           0   (0.0%)   0   (0.0%)  0   (0.0%)
#    Total 104  (19.8%) 148  (28.2%)         119  (22.7%) 108  (20.6%) 45   (8.6%)

report_21st <- structure(list(merchant = c("a", "b", "c", "d", "e", "f", "g", 
"h", "i", "j", "k", "l"), Status = c("processing", "processing", 
"success", "Transaction declined", "success", "pending", "Transaction declined", 
"Success", "Transaction declined", "success", "pending", "processing"
), Face.value = c(10L, 5L, 40L, 30L, 32L, 21L, 23L, 45L, 66L, 
76L, 87L, 89L)), row.names = c(NA, -12L), class = "data.frame")

最新更新