DataFrame/Aggregate
compute aggregate statistics on a DataFrame
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
Aggregate( data, bycolumn, options )
data
-
DataFrame
bycolumn
name, string, or integer; gives the column of the DataFrame used to split the data set into subsets
options
(optional) equation(s) of the form option=value where option can be any of bounds, columns, function or tally.
bounds : By default, Aggregate uses the SplitByColumn command to creates a separate sub-DataFrame for every discrete level in the column given by bycolumn. If the data in the bycolumn column is more or less continuous, the bounds option can be used to specify boundaries of bins into which values are collected. There are several different ways to use this option:
bounds = none is the default and leads to a separate bin for every value in the key column.
bounds = [b0, b1, ..., bn] means values from the key column will be divided into n bins: from b0 to b1, from b1 to b2, etc. Values less than b0 or greater than bn, and the rows containing them, are discarded.
bounds = quantiles(q0, q1, ..., qn) or bounds = deciles(d0, d1, ..., dn) or bounds = percentiles(p0, p1, ..., pn) or bounds = quartiles(q0, q1, ..., qn) are equivalent ways of specifying bins similar to specifying a list, but instead of specifying absolute numbers for the bounds, you specify which fraction of the data should fall in each of the bins. For example, specifying bounds = percentiles(0, 25, 60, 90) specifies three bins, the first of which contains the lowest 25% of data, then then next 60% - 25% = 35% of data, and finally the following 90% - 60% = 30% of data. That is, the bounds are at the 0th, 25th, 60th, and 90th percentile of the data in the key column. This could also be specified as bounds = quantiles(0, 0.25, 0.6, 0.9) using the notion of Quantile instead of Percentile, or bounds = deciles(0, 2.5, 6, 9) using Deciles, or bounds = quartile(0, 1, 2.4, 3.6) using Quartiles.
columns : list; list of numeric values corresponding to the columns or column names. The default is to show all columns.
function : procedure or list(procedure,options); controls the summary statistic or procedure that is applied to the subsets of data. The default is Statistics:-Mean. If function is given as a list, the first element corresponds to the command used to compute the aggregate statistic, and any additional arguments are passed to the given command.
tally : truefalse; controls the display of an additional column in the aggregate DataFrame containing tallies for each level for the given option ByColumn. The default is false.
The Aggregate command returns aggregate statistics on a DataFrame for each distinct level (factor) in a given column.
Aggregate statistics are calculated by splitting the rows of a DataFrame by each factor in a given column into subsets and computing summary statistics for each of the subsets of data.
The output from the Aggregate command is a DataFrame that contains only one row per factor in the given column along with the requested summary statistic for each respective other column.
DF≔DataFrame⁡1,1,2,2,2,3|5,3,6,2,1,4|6,5,1,4,9,9|4,2,1,7,8,3,columns=ID,X1,X2,X3
DF≔IDX1X2X3115642135232611422475219863493
The following returns the mean of all other columns for each distinct level in the column, ID.
Aggregate⁡DF,ID
IDX1X2X3114.5.500000000000003.223.4.666666666666675.33333333333333334.9.3.
Adding the columns option controls which columns are returned. Additionally, the tally option returns a tally for each of the factors.
Aggregate⁡DF,ID,columns=X1,X2
IDX1X2114.5.50000000000000223.4.66666666666667334.9.
Aggregate⁡DF,ID,columns=2,3,tally
IDX1X2Tally114.5.500000000000002223.4.666666666666673334.9.1
The function option allows for the specification of any command that can be applied to a DataSeries. For example, the Statistics:-Median command computes the median for each of the factors of ID.
Aggregate⁡DF,ID,columns=X1,function=Statistics:-Median
IDX1114.222.334.
The Append command appends a column on the right side of the original DataFrame.
Append⁡DF,3,5,7,9,undefined,13,label=X4
IDX1X2X3X411564321352532611742247952198undefined6349313
If the function option is given as a list, supplementary arguments can be passed to the applied command.
Aggregate⁡DF,ID,function=Statistics:-Quartile,2,ignore=true
IDX1X2X3114.5.500000000000003.222.4.7.334.9.3.
The bounds option makes it possible to customize the bins used for the aggregate statistics. In the following, values for the ID column between 1 and 2 are put into the first bin and values 2 and above are put into another.
Aggregate⁡DF,ID,bounds=1,2,3,tally
IDX1X2X3Tally114.5.500000000000003.2223.250000000000005.750000000000004.750000000000004
The DataFrame/Aggregate command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
See Also
Statistics:-SplitByColumn
Download Help Document