Skip to content
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

Closed
travis-leith opened this issue Nov 20, 2020 · 4 comments

Comments

@travis-leith
Copy link

travis-leith commented Nov 20, 2020

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)

@travis-leith travis-leith changed the title Add ability to specify different functions for different columns in summary rows Add ability for summary functions to refer to columns than the output column Nov 20, 2020
@travis-leith travis-leith changed the title Add ability for summary functions to refer to columns than the output column Add ability for summary functions to refer to columns other than the output column Nov 20, 2020
@rich-iannone
Copy link
Member

This is a great idea, thanks for posting it here. Totally agree that summarizing is a bit simplistic right now.

@ghost
Copy link

ghost commented Jan 12, 2021

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.

@rich-iannone rich-iannone modified the milestones: v0.3.1, v0.4.0 Jun 2, 2021
@nicomunting
Copy link

nicomunting commented Dec 21, 2021

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 weighted.mean function. This would not work for tables with grouped rows though.

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?

@rich-iannone rich-iannone modified the milestones: v0.4.0, v0.5.0 Jan 26, 2022
@rich-iannone rich-iannone modified the milestones: v0.5.0, v0.6.0 Mar 1, 2022
@rich-iannone rich-iannone modified the milestones: v0.6.0, v0.7.0 Apr 19, 2022
@rich-iannone rich-iannone modified the milestones: v0.7.0, v0.8.0 Jul 7, 2022
@rich-iannone
Copy link
Member

Now solved with #1018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants