-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability for summary functions to refer to columns other than the output column #690
Comments
This is a great idea, thanks for posting it here. Totally agree that summarizing is a bit simplistic right now. |
Yes, I need this, for example, for calculating a rate. I have a column with "number of cases", and another column with rate, split by region (where rate is cases/population for a given region). To calculate an overall rate for my summary, I need (sum cases)/(sum population). Not possible to do it with just the vector of the individual column. Thank you for your work on this. |
I ran into the same limitation. It would be nice if this capability was added in v0.4.0. As a workaround (or dirty hack) I just passed a vector with the weights directly to the df <- tibble::tribble(
~price, ~value,
5, 4,
2, 6,
3, 1
)
df %>%
dplyr::mutate(value_pct = value / price) %>%
gt() %>%
summary_rows(fns = list(total = ~ sum(.)), columns = c(value, price)) %>%
summary_rows(
fns = list(total = ~ weighted.mean(., w = df$price)),
columns = value_pct,
formatter = fmt_percent, decimals = 0
) %>%
fmt_percent(columns = contains("pct"), decimals = 0) By the way, is this not a duplicate of #383? |
Now solved with #1018 |
In my experience it is uncommon to want to have a summary row in a table that uses a univariate function for all columns (as is the case in gt now - v0.2.2).
More commonly I want to apply, for example, a sum to some columns (such as trade value) and a weighted average to others (such as average trade price. In order to do this I would need a way for those functions to reference columns other than that for which the output is for. Such as the summary for column x should be
sum(x*y)/sum(y)
The text was updated successfully, but these errors were encountered: