-
Notifications
You must be signed in to change notification settings - Fork 217
Closed
Milestone
Description
Here's an easy example. I have a gt table where I get the sum of a variety of columns. One of the columns is for % of value. I'd like to get the % of value based on the sums in the Summary/Grand Summary Row as well.
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
df <-
tibble::tribble(
~price, ~value,
5, 4,
2, 6,
3, 1
)
df <-
df %>%
mutate(pct_of_value = value / price)
df
#> # A tibble: 3 x 3
#> price value pct_of_value
#> <dbl> <dbl> <dbl>
#> 1 5 4 0.8
#> 2 2 6 3
#> 3 3 1 0.333
summary_row <-
df %>%
summarise_at(vars(price, value), sum) %>%
mutate(pct_of_value = value / price)
summary_row
#> # A tibble: 1 x 3
#> price value pct_of_value
#> <dbl> <dbl> <dbl>
#> 1 10 11 1.1
Created on 2019-10-24 by the reprex package (v0.3.0)
Reactions are currently unavailable