Difference between revisions of "Aggregate"

Line 50: Line 50:
  
 
= Default Value =
 
= Default Value =
 +
 +
It is possible that no data maps to a particular value/position in «targetIndex».  This can occur when that value (or position) never occurs within «map», or when all data in «X» that would map to that position is [[Null]]-valued.  (Note: Aggregate ignores null values in «X»).
 +
 +
For result cells with no data, [[«null»]] is returned.  No warning is issued when there are result cells with no data.  If you'd like to use a different value, such as 0, you can specify the value in the optional «defaultValue» parameter, e.g.:
 +
 +
[[Aggregate]](X,map,I,J,type:"SDeviation",defaultValue:0)
  
 
= Inverses of Aggregate =
 
= Inverses of Aggregate =

Revision as of 09:48, 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

It is possible that no data maps to a particular value/position in «targetIndex». This can occur when that value (or position) never occurs within «map», or when all data in «X» that would map to that position is Null-valued. (Note: Aggregate ignores null values in «X»).

For result cells with no data, «null» is returned. No warning is issued when there are result cells with no data. If you'd like to use a different value, such as 0, you can specify the value in the optional «defaultValue» parameter, e.g.:

Aggregate(X,map,I,J,type:"SDeviation",defaultValue:0)

Inverses of Aggregate

See Also

Comments


You are not allowed to post comments.