使用adorn_totals()按列指定计算。

huangapple go评论95阅读模式
英文:

Specify the calculation by column using adorn_totals()

问题

我正在尝试使用R中的Janitor包添加一个总计行,但是我需要使用sum函数对两列进行总计,而另一列应该是一个百分比(而不是该列的总和)。

```R
library(tidyverse)
library(janitor)

df.1 <- tribble(
~customer  ,~period, ~cost1, ~cost2 ,
'cust1',  '202201', 5, 10,
'cust1',  '202202', 5, 10,
'cust1',  '202203', 5, 10,
'cust1',  '202204', 5, 10,
)

df.1 %>%
  group_by(customer, period) %>%
  summarise(cost1 = sum(cost1, na.rm = T),
            cost2 = sum(cost2, na.rm = T),
            total = cost1 + cost2,
            pct   = cost1 / cost2) %>%
  adorn_totals(where = 'row')

预期输出应为:

customer    period    cost1    cost2    total    pct
cust1       202201       5        10        15   .33333
cust1       202202       5        10        15   .33333
cust1       202203       5        10        15   .33333
cust1       202204       5        10        15   .33333  
Total                   20        40        60   .33333

提前感谢您的建议。


<details>
<summary>英文:</summary>

I am trying to add a totals row usin ghte Janitor package in R, however I need 2 columns to be totaled using the sum function and one column to be a percentage (not the sum of the column).

library(tidyverse)
library(janitor)

df.1 <- tribble(
~customer ,~period, ~cost1, ~cost2 ,
'cust1', '202201', 5, 10,
'cust1', '202202', 5, 10,
'cust1', '202203', 5, 10,
'cust1', '202204', 5, 10,
)

df.1 %>%
group_by(customer, period) %>%
summarise(cost1 = sum(cost1, na.rm = T),
cost2 = sum(cost2, na.rm = T),
total = cost1 + cost2,
pct = cost1 / cost2) %>%
adorn_totals(where = 'row')


Expected output would be:

customer period cost1 cost2 total pct
cust1 202201 5 10 15 .33333
cust1 202202 5 10 15 .33333
cust1 202203 5 10 15 .33333
cust1 202204 5 10 15 .33333
Total 20 40 60 .33333


Thanks in advance for advice.

</details>


# 答案1
**得分**: 2

在 `adorn` 之后,按行获取百分比:

```R
df.1 %>%
  group_by(customer, period) %>%
  summarise(cost1 = sum(cost1, na.rm = T),
            cost2 = sum(cost2, na.rm = T),
            total = cost1 + cost2) %>%
  adorn_totals(where = 'row') %>%
  mutate(pct = cost1/total)
# `summarise()` has grouped output by 'customer'. You can override using the
# `.groups` argument.
# customer period cost1 cost2 total       pct
#    cust1 202201     5    10    15 0.3333333
#    cust1 202202     5    10    15 0.3333333
#    cust1 202203     5    10    15 0.3333333
#    cust1 202204     5    10    15 0.3333333
#    Total      -    20    40    60 0.3333333

请注意,这是R代码的一部分,只提供了翻译,不包含其他内容。

英文:

Get the percentage per row, after adorn:

df.1 %&gt;% 
  group_by(customer, period) %&gt;% 
  summarise(cost1 = sum(cost1, na.rm = T),
            cost2 = sum(cost2, na.rm = T),
            total = cost1 + cost2) %&gt;% 
  adorn_totals(where = &#39;row&#39;) %&gt;% 
  mutate(pct = cost1/total)
# `summarise()` has grouped output by &#39;customer&#39;. You can override using the
# `.groups` argument.
# customer period cost1 cost2 total       pct
#    cust1 202201     5    10    15 0.3333333
#    cust1 202202     5    10    15 0.3333333
#    cust1 202203     5    10    15 0.3333333
#    cust1 202204     5    10    15 0.3333333
#    Total      -    20    40    60 0.3333333

huangapple
  • 本文由 发表于 2023年6月5日 23:13:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定