jtablesaw / tablesaw

Java dataframe and visualization library
https://jtablesaw.github.io/tablesaw/
Apache License 2.0
3.54k stars 643 forks source link

Multiple Aggregate #251

Closed YuanZhencai closed 6 years ago

YuanZhencai commented 6 years ago

csv data

Team,League,Year,RS,RA,W,OBP,SLG,BA,Playoffs,RankSeason,RankPlayoffs,G,OOBP,OSLG
ARI,NL,2012,734,688,81,0.328,0.418,0.259,0,,,162,0.317,0.415
ATL,NL,2012,700,600,94,0.32,0.389,0.247,1,4,5,162,0.306,0.378
BAL,AL,2012,712,705,93,0.311,0.417,0.247,1,5,4,162,0.315,0.403
BAL,AL,2011,708,860,69,0.316,0.413,0.257,0,,,162,0.341,0.46
BOS,AL,2011,875,737,90,0.349,0.461,0.28,0,,,162,0.322,0.392
CHC,NL,2011,654,756,71,0.314,0.401,0.256,0,,,162,0.335,0.413
CHW,AL,2011,654,706,79,0.319,0.388,0.252,0,,,162,0.317,0.397
CIN,NL,2011,735,720,79,0.326,0.408,0.256,0,,,162,0.325,0.415
CLE,AL,2011,704,760,80,0.317,0.396,0.25,0,,,162,0.323,0.413

how to multiple aggregate

I want below code effect

select max(RS),min(RS),max(RA),min(RA) from data group by Team

then I found a bug

my code sample

Table table = Table.read().csv("baseball.csv");
ViewGroup group = ViewGroup.create(table, "Team");

Map<String, AggregateFunction> functions = new HashMap<>();

functions.put("RS", AggregateFunctions.max);
functions.put("RA", AggregateFunctions.min);

NumericSummaryTable agg = group.agg(functions);

System.out.println(agg.print());

error

java.lang.IndexOutOfBoundsException: Index (8) is greater than or equal to list size (8)

    at it.unimi.dsi.fastutil.doubles.DoubleArrayList.getDouble(DoubleArrayList.java:261)
    at tech.tablesaw.api.DoubleColumn.getString(DoubleColumn.java:378)
    at tech.tablesaw.api.Table.get(Table.java:416)
    at tech.tablesaw.io.string.DataFramePrinter.getDataTokens(DataFramePrinter.java:132)
    at tech.tablesaw.io.string.DataFramePrinter.print(DataFramePrinter.java:53)
    at tech.tablesaw.table.Relation.print(Relation.java:178)
    at tech.tablesaw.table.Relation.print(Relation.java:183)
lwhite1 commented 6 years ago

I don't yet understand the problem with the code you called, but you can get the results you want using a new method.

` Table table = Table.read().csv("baseball.csv"); Map<AggregateFunction, Double> rs = aggAll("RS", min, max); Map<AggregateFunction, Double> ra = aggAll("RA", min, max);

` and then get your results from the maps.

lwhite1 commented 6 years ago

In tablesaw 0.2, the code would look like this

Table table = Table.read().csv("baseball.csv");
Table result = table.summarize("RS", "RA", min, max).apply();