英文:
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 %>%
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论