Difference between revisions of "Aggregate"
(Initial description) |
|||
Line 21: | Line 21: | ||
Revenue_by_year := [[Aggregate]](Revenue_by_month,MonthsToYears,Months,Years) | Revenue_by_year := [[Aggregate]](Revenue_by_month,MonthsToYears,Months,Years) | ||
− | = | + | = Aggregation types = |
+ | |||
+ | The optional «type» parameter specifies the aggregation type. The parameter names an existing [[:Category:Array-reducing functions|array-reducing function]], either built-in or [[User-Defined Functions|user-defined]]. You can supply either the textual name of the function, or a handle to the function (or even an array to these). Built-in functions that can be applied include [[Sum]] (default), [[Max]], [[Min]], [[Average]] or [[Mean]], [[Product]], [[Median]], [[SDeviation]], [[Variance]], [[Skewness]], [[Kurtosis]], and [[Join]]. | ||
+ | |||
+ | Revenue_dev := [[Aggregate]](Revenue_by_month,MonthToYear,Month,Year,Type:"SDeviation") | ||
+ | Peak_revenue:= [[Aggregate]](Revenue_by_month,MonthToYear,Month,Year,Type:[[Handle]](Max)) | ||
+ | Ave_revenue := [[Aggregate]](Revenue_by_month,MonthToYear,Month,Year,Type:"Average") | ||
+ | |||
+ | You can also create your own custom aggregation method in the form of a [[User-Defined Function]]. The function should accept an array parameter and an index, and should [[:Category:Array-reducing functions|reduce]] the array so that the result eliminates the indicated index. | ||
+ | |||
+ | The following would all be examples of legal aggregation functions: | ||
+ | Function First(A:Array[I] ; I:Index) := A[@I=1] | ||
+ | Function Last(A:Array[I] ; I:Index) := A[@I=[[Size]](I)] | ||
+ | Function Middle(A:Array[I] ; I:Index) := A[@I=[[Floor]]([[Size]](I)/2)+1] | ||
+ | Function Count(A:Array[I] ; I:Index) := [[Size]](I) | ||
+ | Function PosMax(A:Array[I] ; I:Index) := [[ArgMax]](A,I,position:true) | ||
+ | |||
+ | These would be used in the same fasion as other types: | ||
+ | Opening_Revenue := [[Aggregate]](Revenue_by_month,MonthToYear,Month,Year,Type:"First") | ||
+ | Closing_Revenue := [[Aggregate]](Revenue_by_month,MonthToYear,Month,Year,Type:"Last") | ||
+ | Best_Month := [[Aggregate]](Revenue_by_month,MonthToYear,Month,Year,Type:Handle(PosMax)) | ||
+ | |||
+ | |||
+ | |||
+ | Note: Aggregation (aka conglomeration) functions can also be utilized by the [[MdTable]] function. The requirements are the same. | ||
= Positional Maps = | = Positional Maps = |
Revision as of 09:44, 6 March 2009
New to Analytica 4.2
Aggregate( X,map : Array[I] ; I, targetIndex : Index )
Aggregate converts an array «X» indexed by a fine-grained index «I» (e.g., Months) into an array indexed by a coarser index «targetIndex» (e.g., Years), by aggregating all the elements that map to the same «targetIndex» value. The «map» parameter specifies how the elements of «I» map to the elements of «targetIndex». Each cell of «map» should be an element of «targetIndex» (or, if you also specify an optional positional:true parameter, then each element of «map» can specify an integer position along «targetIndex»).
In its standard usage, Aggregate sums all elements from «X» that map to the same element along «targetIndex». However, the type of aggregation can be changed using an optional «type» parameter, containing the name of or handle to any valid aggregation function, such as Max, Min, Product, Average or Median.
Library
Array functions
Examples
Index Months := Sequence(MakeDate(2009,1,1),MakeDate(2020,1,1),dateUnit:"M") Index Years := 2009..2020 MonthsToYears := DatePart(Months,"Y") Revenue_by_month := { array indexed by Months } Revenue_by_year := Aggregate(Revenue_by_month,MonthsToYears,Months,Years)
Aggregation types
The optional «type» parameter specifies the aggregation type. The parameter names an existing array-reducing function, either built-in or user-defined. You can supply either the textual name of the function, or a handle to the function (or even an array to these). Built-in functions that can be applied include Sum (default), Max, Min, Average or Mean, Product, Median, SDeviation, Variance, Skewness, Kurtosis, and Join.
Revenue_dev := Aggregate(Revenue_by_month,MonthToYear,Month,Year,Type:"SDeviation") Peak_revenue:= Aggregate(Revenue_by_month,MonthToYear,Month,Year,Type:Handle(Max)) Ave_revenue := Aggregate(Revenue_by_month,MonthToYear,Month,Year,Type:"Average")
You can also create your own custom aggregation method in the form of a User-Defined Function. The function should accept an array parameter and an index, and should reduce the array so that the result eliminates the indicated index.
The following would all be examples of legal aggregation functions:
Function First(A:Array[I] ; I:Index) := A[@I=1] Function Last(A:Array[I] ; I:Index) := A[@I=Size(I)] Function Middle(A:Array[I] ; I:Index) := A[@I=Floor(Size(I)/2)+1] Function Count(A:Array[I] ; I:Index) := Size(I) Function PosMax(A:Array[I] ; I:Index) := ArgMax(A,I,position:true)
These would be used in the same fasion as other types:
Opening_Revenue := Aggregate(Revenue_by_month,MonthToYear,Month,Year,Type:"First") Closing_Revenue := Aggregate(Revenue_by_month,MonthToYear,Month,Year,Type:"Last") Best_Month := Aggregate(Revenue_by_month,MonthToYear,Month,Year,Type:Handle(PosMax))
Note: Aggregation (aka conglomeration) functions can also be utilized by the MdTable function. The requirements are the same.
Positional Maps
Default Value
Inverses of Aggregate
See Also
- Subscript-Slice Operator -- standard reindexing
- Frequency -- basically aggregation with "count" type
Enable comment auto-refresher