Difference between revisions of "Transforming functions"
Line 210: | Line 210: | ||
''See [[Array Function Example Variables]] for example array variables used here and below.'' | ''See [[Array Function Example Variables]] for example array variables used here and below.'' | ||
− | ==CumProduct(x, i, passNull, reset)== | + | ==CumProduct(x, i, ''passNull, reset'')== |
Returns an array with each element being the product of all of the elements of '''x''' along dimension '''i''' up to, and including, the corresponding element of '''x'''. | Returns an array with each element being the product of all of the elements of '''x''' along dimension '''i''' up to, and including, the corresponding element of '''x'''. | ||
Line 234: | Line 234: | ||
| 1.061 | | 1.061 | ||
| 1.104 | | 1.104 | ||
+ | |} | ||
+ | |||
+ | ==Rank(x, i, ''type, keyIndex, descending, caseInsensitive, passNaNs, passNulls'')== | ||
+ | '''Rank(x,i) returns an array of the rank values of '''x''' across index '''i'''. The lowest value in '''x''' has a rank value of 1, the next-lowest has a rank value of 2, and so on. '''i''' is optional if '''x''' is one-dimensional. | ||
+ | |||
+ | If '''i''' is omitted when '''x''' is more than one-dimensional, the innermost dimension is ranked. | ||
+ | If two (or N) values are equal, they receive the same rank and the next higher value receives a | ||
+ | rank 2 (or N) higher. You can use an optional parameter, '''Type''', to control which rank is assigned | ||
+ | to equal values. By default, the lowest rank is used, equivalent to '''Rank(x, i, Type: -1)'''. Alternatively, '''Rank(x ,i, Type: 0)''' uses the mid-rank and Rank(x, i, Type: 1)''' uses the upper-rank. | ||
+ | '''Rank(x, i, Type: Null)''' assigns a unique rank to every element (the numbers 1 thru N) in which tied elements may have different ranks. | ||
+ | |||
+ | A multi-key rank can be processed by indexing each key with a new index, and specifying this | ||
+ | index for the optional '''keyIndex''' parameter. In a multi-key rank, <code>x[@KeyIndex = 1]</code> determines the rank order, except that ties are then resolved using <code>x[@KeyIndex = 2]</code>, any ties there are resolved using <code>x[@KeyIndex = 3]</code>, and so on. | ||
+ | |||
+ | '''Rank(x, i, descending: true)''' assigns the largest value a rank 1, the second largest a rank 2, and | ||
+ | so on. When '''x''' contains textual values, the optional boolean parameter '''caseInsensitive: true''' | ||
+ | ignores upper-lower case differences during the comparisons. The parameters '''descending''' and | ||
+ | '''caseInsensitive''' may also be indexed by they''' keyIndex''' when they vary by key. | ||
+ | |||
+ | By default, '''Rank''' assigns an arbitrary ranking to <code>NaN</code> or <code>Null</code> values. Alternatively, you can pass these through to the result as <code>NaN</code> or <code>Null</code> using '''Rank(x, i, passNaNs: true, passNulls: true)'''. | ||
+ | |||
+ | '''Library:''' Array | ||
+ | |||
+ | '''Examples:''' Basic example: | ||
+ | |||
+ | <code>Rank(Years) →</code> | ||
+ | {| class="wikitable" border="1" | ||
+ | ! colspan="5" style="text-align: left;" | Years ▶ | ||
+ | |- | ||
+ | ! style="width:75px;" |'''2005 ''' | ||
+ | ! style="width:75px;" |'''2006 ''' | ||
+ | ! style="width:75px;" |'''2007 ''' | ||
+ | ! style="width:75px;" |'''2008 ''' | ||
+ | ! style="width:75px;" |'''2009 ''' | ||
+ | |- | ||
+ | | 1 | ||
+ | | 2 | ||
+ | | 3 | ||
+ | | 4 | ||
+ | | 5 | ||
+ | |} | ||
+ | |||
+ | <code>Rank(Car_prices, Car_type) →</code> | ||
+ | {| class="wikitable" border="1" | ||
+ | ! !! colspan="5" style="text-align: left;" | Years ▶ | ||
+ | |- | ||
+ | ! style="width:100px;" |'''Car_type ▼ ''' | ||
+ | ! style="width:75px;" |'''2005 ''' | ||
+ | ! style="width:75px;" |'''2006 ''' | ||
+ | ! style="width:75px;" |'''2007 ''' | ||
+ | ! style="width:75px;" |'''2008 ''' | ||
+ | ! style="width:75px;" |'''2009 ''' | ||
+ | |- | ||
+ | !'''VW''' | ||
+ | | 1 | ||
+ | | 1 | ||
+ | | 1 | ||
+ | | 1 | ||
+ | | 1 | ||
+ | |- | ||
+ | !'''Honda ''' | ||
+ | | 2 | ||
+ | | 2 | ||
+ | | 2 | ||
+ | | 2 | ||
+ | | 2 | ||
+ | |- | ||
+ | !'''BMW''' | ||
+ | | 3 | ||
+ | | 3 | ||
+ | | 3 | ||
+ | | 3 | ||
+ | | 3 | ||
+ | |} | ||
+ | |||
+ | Optional '''Type''' parameter example: | ||
+ | |||
+ | <code>Index RankType := [-1, 0, 1, Null]</code> | ||
+ | |||
+ | <code>Rank(NumRepairs, CarNum,Type: RankType) →</code> | ||
+ | |||
+ | {| class="wikitable" border="1" | ||
+ | ! !! colspan="7" style="text-align: left;" | CarNum ▶ | ||
+ | |- | ||
+ | ! Rank_type ▼ | ||
+ | ! style="width:35px;" |'''1 ''' | ||
+ | ! style="width:35px;" |'''2 ''' | ||
+ | ! style="width:35px;" |'''3 ''' | ||
+ | ! style="width:35px;" |'''4 ''' | ||
+ | ! style="width:35px;" |'''5 ''' | ||
+ | ! style="width:35px;" |6 | ||
+ | ! style="width:35px;" |7 | ||
+ | |- | ||
+ | ! -1 | ||
+ | | 7 | ||
+ | | 2 | ||
+ | | 6 | ||
+ | | 2 | ||
+ | | 2 | ||
+ | | 1 | ||
+ | | 2 | ||
+ | | style="border-width: 0px" | Lowest rank for duplicates, 2 (default) | ||
+ | |- | ||
+ | ! 0 | ||
+ | | 7 | ||
+ | | 3.5 | ||
+ | | 6 | ||
+ | | 3.5 | ||
+ | | 3.5 | ||
+ | | 1 | ||
+ | | 3.5 | ||
+ | | style="border-width: 0px" | Mid rank for duplicates, 3.5 | ||
+ | |- | ||
+ | ! 1 | ||
+ | | 7 | ||
+ | | 5 | ||
+ | | 6 | ||
+ | | 5 | ||
+ | | 5 | ||
+ | | 1 | ||
+ | | 5 | ||
+ | | style="border-width: 0px" | Upper rank for duplicates, 5 | ||
+ | |- | ||
+ | ! Null | ||
+ | | 7 | ||
+ | | 2 | ||
+ | | 6 | ||
+ | | 3 | ||
+ | | 4 | ||
+ | | 1 | ||
+ | | 5 | ||
+ | | style="border-width: 0px" | Unique rank for duplicates | ||
|} | |} | ||
==See Also== | ==See Also== | ||
<footer>Array-reducing functions / {{PAGENAME}} / Relational tables and multiD arrays</footer> | <footer>Array-reducing functions / {{PAGENAME}} / Relational tables and multiD arrays</footer> |
Revision as of 23:03, 11 December 2015
A transforming function operates across a dimension of an array and returns a result that has the same dimensions as its input array.
The function Cumulate(x, i) illustrates some properties of transforming functions.
Example:
Cumulate(Car_prices, Years) →
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 2005 | 2006 | 2007 | 2008 | 2009 |
VW | 16K | 33K | 51K | 70K | 90K |
Honda | 18K | 37K | 57K | 79K | 103K |
BMW | 25K | 51K | 79K | 109K | 141K |
The second parameter, i, specifying the dimension over which to cumulate, is optional. But if the array, x, has more than one dimension, Analytica might not cumulate over the dimension you expect. For this reason, it is safer always to specify the dimension index explicitly in any transforming function.
Cumulate(x, i, passNull, reset)
Returns an array with each element being the sum of all of the elements of x along dimension i up to, and including, the corresponding element of x.
Cumulate(1, i)
is equivalent to @i
, where each numbers the elements of an index.
The optional passNull parameter controls now null
values in x are passed through to the result.If passNull is false or omitted, then null values in x are ignored and do not effect the cumulation. Leading null
values will be passed through, but after a numeric value is encountered, null
values in x will cumulate the same as zero.
The optional reset parameter, an array of boolean values along i, can be used to indicate points along i where you want to restart the cumulation. For example, if you want to restart the cumulation following a state change, reset can be set to true each time a new state is entered.
Library: Array
Example:
Cumulate(Cost_of_ownership, Time) →
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 0 | 1 | 2 | 3 | 4 |
VW | 2810 | 5761 | 8859 | 12.11K | 15.53K |
Honda | 3535 | 7382 | 11.28K | 15.45K | 19.81K |
BMW | 3185 | 6479 | 9888 | 13.42K | 17.07K |
Cumulate(Cost_of_ownership, Car_type, reset: Time = 2) →
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 0 | 1 | 2 | 3 | 4 |
VW | 2810 | 5761 | 3098 | 6351K | 9767K |
Honda | 3535 | 7382 | 3897K | 8063K | 12.43K |
BMW | 3185 | 6479 | 3409 | 6938K | 10.59K |
See Array Function Example Variables for example array variables used here and below.
Uncumulate(x, i, firstElement)
Uncumulate(x, i) returns an array whose first element (along i) is the first element of x, and each other element is the difference between the corresponding element of x and the previous element of x. Uncumulate(x, i, firstElement) returns an array with the first element along i equal to firstElement, and each other element equal to the difference between the corresponding element of x and the previous element of x.
Uncumulate(x, i) is the inverse of Cumulate(x, i). Uncumulate(x, i, 0) is similar to a discrete differential operator.
Library: Array
Example:
Uncumulate(Cost_of_ownership, Time) →
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 0 | 1 | 2 | 3 | 4 |
VW | 2810 | 141 | 147 | 155 | 163 |
Honda | 3535 | 312 | 50 | 269 | 199 |
BMW | 3185 | 109 | 115 | 120 | 127 |
Uncumulate(Cost_of_ownership, Time,0) →
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 0 | 1 | 2 | 3 | 4 |
VW | 0 | 141 | 147 | 155 | 163 |
Honda | 0 | 312 | 50 | 269 | 199 |
BMW | 0 | 109 | 115 | 120 | 127 |
See Array Function Example Variables for example array variables used here and below.
CumProduct(x, i, passNull, reset)
Returns an array with each element being the product of all of the elements of x along dimension i up to, and including, the corresponding element of x.
For a description of the optional parameters, passNull and reset, see function Cumulate(x, i, passNull, reset).
Library: Array
Example:
Cumproduct(Rate_of_inflation, Years) →
Years ▶ | ||||
---|---|---|---|---|
2005 | 2006 | 2007 | 2008 | 2009 |
1 | 1.01 | 1.03 | 1.061 | 1.104 |
Rank(x, i, type, keyIndex, descending, caseInsensitive, passNaNs, passNulls)
Rank(x,i) returns an array of the rank values of x across index i. The lowest value in x has a rank value of 1, the next-lowest has a rank value of 2, and so on. i is optional if x is one-dimensional.
If i is omitted when x is more than one-dimensional, the innermost dimension is ranked. If two (or N) values are equal, they receive the same rank and the next higher value receives a rank 2 (or N) higher. You can use an optional parameter, Type, to control which rank is assigned to equal values. By default, the lowest rank is used, equivalent to Rank(x, i, Type: -1). Alternatively, Rank(x ,i, Type: 0) uses the mid-rank and Rank(x, i, Type: 1) uses the upper-rank. Rank(x, i, Type: Null) assigns a unique rank to every element (the numbers 1 thru N) in which tied elements may have different ranks.
A multi-key rank can be processed by indexing each key with a new index, and specifying this
index for the optional keyIndex parameter. In a multi-key rank, x[@KeyIndex = 1]
determines the rank order, except that ties are then resolved using x[@KeyIndex = 2]
, any ties there are resolved using x[@KeyIndex = 3]
, and so on.
Rank(x, i, descending: true) assigns the largest value a rank 1, the second largest a rank 2, and so on. When x contains textual values, the optional boolean parameter caseInsensitive: true ignores upper-lower case differences during the comparisons. The parameters descending and caseInsensitive may also be indexed by they keyIndex when they vary by key.
By default, Rank assigns an arbitrary ranking to NaN
or Null
values. Alternatively, you can pass these through to the result as NaN
or Null
using Rank(x, i, passNaNs: true, passNulls: true).
Library: Array
Examples: Basic example:
Rank(Years) →
Years ▶ | ||||
---|---|---|---|---|
2005 | 2006 | 2007 | 2008 | 2009 |
1 | 2 | 3 | 4 | 5 |
Rank(Car_prices, Car_type) →
Years ▶ | |||||
---|---|---|---|---|---|
Car_type ▼ | 2005 | 2006 | 2007 | 2008 | 2009 |
VW | 1 | 1 | 1 | 1 | 1 |
Honda | 2 | 2 | 2 | 2 | 2 |
BMW | 3 | 3 | 3 | 3 | 3 |
Optional Type parameter example:
Index RankType := [-1, 0, 1, Null]
Rank(NumRepairs, CarNum,Type: RankType) →
CarNum ▶ | ||||||||
---|---|---|---|---|---|---|---|---|
Rank_type ▼ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | |
-1 | 7 | 2 | 6 | 2 | 2 | 1 | 2 | Lowest rank for duplicates, 2 (default) |
0 | 7 | 3.5 | 6 | 3.5 | 3.5 | 1 | 3.5 | Mid rank for duplicates, 3.5 |
1 | 7 | 5 | 6 | 5 | 5 | 1 | 5 | Upper rank for duplicates, 5 |
Null | 7 | 2 | 6 | 3 | 4 | 1 | 5 | Unique rank for duplicates |
Enable comment auto-refresher