Skip to content

Add ability for summary functions to refer to columns other than the output column #690

Closed

Description

@travis-leith

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)

Activity

  1. 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[/+] on Nov 20, 2020
  2. 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[/+] on Nov 20, 2020
  3. rich-iannone commented on Dec 1, 2020

    @rich-iannone
    Member

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

  4. self-assigned this
    on Dec 1, 2020
  5. added this to the v0.3.1 milestone on Dec 1, 2020
  6. modified the milestones: v0.3.1, v0.4.0 on Jun 2, 2021
  7. nicomunting commented on Dec 21, 2021

    @nicomunting

    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?

  8. modified the milestones: v0.4.0, v0.5.0 on Jan 26, 2022
  9. modified the milestones: v0.5.0, v0.6.0 on Mar 1, 2022
  10. modified the milestones: v0.6.0, v0.7.0 on Apr 19, 2022
  11. modified the milestones: v0.7.0, v0.8.0 on Jul 7, 2022
  12. rich-iannone commented on Aug 25, 2022

    @rich-iannone
    Member

    Now solved with #1018

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions