简体   繁体   中英

Vega Transform to add column for total calculated from sum of values from another column

I have a dataset where each row represents a medal won in a certain state for a contest and I am trying to transform it to make a stacked bar chart. It is easy enough to transform the dataset using an aggregate transform to get the total number of medals in each state, or the total number of each type (gold, silver, bronze), but what I would like to do is add a new column to the dataset with the total number of medals in each state. That way if I have 3 rows for each state, each with the total number of bronze, silver, and gold medals, each row also has the total number of medals for that state (which will obviously be the same across each group of three rows).

Here is the header and first five lines of the dataset for reference:

medal,beer_name,brewery,city,state,category,year,id
Gold,Volksbier Vienna,Wibby Brewing,Longmont,CO,American Amber Lager,2020,8
Silver,Oktoberfest,Founders Brewing Co.,Grand Rapids,MI,American Amber Lager,2020,26
Bronze,Amber Lager,Skipping Rock Beer Co.,Staunton,VA,American Amber Lager,2020,51
Gold,Lager at World's End,Epidemic Ales,Concord,CA,American Lager,2020,6
Silver,Seismic Tremor,Seismic Brewing Co.,Santa Rosa,CA,American Lager,2020,6

This is what I have tried:

transform: [
                {
                    type: "aggregate",
                    groupby: ["state","medal"]
                },
                {
                    type: "collect",
                    sort: {
                        field: ["state"],
                        order: ["ascending"]
                    }
                },
                { 
                    //This is the transform I need to change
                    type: "aggregate",
                    groupby: ["state","medal"],
                    fields: ["count"],
                    ops: ["sum"],
                    as: ["total_medals"]
                }
            ]

You need joinaggregate transform. If you post your full working spec, I can provide more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM