Difference between revisions of "Subset"

 
(10 intermediate revisions by 2 users not shown)
Line 5: Line 5:
 
== Subset(d, ''position, i, resultIndex, warnEmpty'') ==
 
== Subset(d, ''position, i, resultIndex, warnEmpty'') ==
  
[[Subset]](d) returns a list containing all the index elements of the index of 1-D array for which «d» is true. The function is used to create a new index that is a subset of an existing index.
+
If «d» is an Index or a 1D array index by «i», [[Subset]](d) returns a list of all the elements of the index for which «d» is true (1). The function is used to create a new index that is a subset of an existing index.
  
The parameter to [[Subset]] must be one-dimensional;  a scalar parameter or a parameter with two or more dimensions results in an error.
+
If «d» is not an Index or 1D array -- if it's scalar or has more than 1D -- it returns an error.
  
 
[[Subset]] in its standard one-parameter usage does not array abstract because it returns an unindexed list.  
 
[[Subset]] in its standard one-parameter usage does not array abstract because it returns an unindexed list.  
  
 
When there is the possibility of having [[Null]] values in your array, you should consider whether you want those elements to be retained.  The following two uses respectively retain or remove [[Null]] elements:
 
When there is the possibility of having [[Null]] values in your array, you should consider whether you want those elements to be retained.  The following two uses respectively retain or remove [[Null]] elements:
[[Subset]](Test [[Or]] Test=[[Null]])
+
 
[[Subset]](Test [[And]] Test<>[[Null]])
+
:<code>Subset(Test Or Test = Null)</code>
 +
:<code>Subset(Test And Test <> Null)</code>
  
 
Without the explicit test, [[Null]] elements are removed.
 
Without the explicit test, [[Null]] elements are removed.
  
== Examples ==
+
== Example ==
Index Years := 2005 .. 2009
+
Let:
  Subset(Years < 2008) &rarr; [2005, 2006, 2007]
+
 
Subset(Years > 2005 and Years < 2008) &rarr; [2006, 2007]
+
:<code>Index Years := 2005..2009</code>
 +
   
 +
Then:
 +
 
 +
:<code>Subset(Years < 2008) &rarr;</code>
 +
::<code>[2005, 2006, 2007]</code>
 +
:<code>Subset(Years > 2005 and Years < 2008) &rarr;</code>
 +
::<code>[2006, 2007]</code>
  
 
==Optional parameters==
 
==Optional parameters==
 
===Position===
 
===Position===
The optional «position» parameter specifies whether the index element or the index position should be returned (see [[Associative vs. Positional Indexing]]).  When «position» is omitted or when <code>position: false</code> is specified, the index element is returned.  <code>'''position: true'''</code> returns the positions of each non-zero element along «d»’s index for which «d»’s values are true:
+
The optional «position» parameter specifies whether the index element or the index position should be returned (see [[Associative vs. Positional Indexing]]).  When «position» is omitted or when <code>position: false</code> is specified, the index element is returned.  <code>'''position: true'''</code> returns the positions of each non-zero element along «d»’s index for which «d»’s values are true: <code>Subset(D, position: true)</code>
 +
 
 +
The need to specify positions arises when your index contains duplicate values, or when the input to the function is an array with an [[implicit index]].
 +
 
 +
In general, the one-dimensional array passed to [[Subset]] needs to be explicitly indexed because the index elements corresponding to true elements is returned.  Thus, an implicitly indexed array (e.g., a list) also results in an error.  Theoretically [[Subset]] could return a result in the implicitly indexed case, but it would be misleading since it would just be returning all the true values, for example:
  
[[Subset]](D, position:true)
+
:<code>Subset(1997..2008 > 2005) &rarr;? [1, 1, 1]    { Hypothetical only -- error reported }</code>
  
The need to specify positions arises when your index contains duplicate values, or when the input to the function is an array with an implicit index.
+
In a case like this, you should use a [[Local Indexes|local index]], e.g.:
  
In general, the one-dimensional array passed to [[Subset]] needs to be explicitly indexed because the index elements corresponding to true elements is returned.  Thus, an implicitly indexed array (e.g., a list) also results in an error.  Theoretically [[Subset]] could return a result in the implicitly indexed case, but it would be misleading since it would just be returning all the true values, for example:
+
:<code>Index I := 1997..2008 do Subset(I > 2005) &rarr; [2006, 2007, 2008]</code>
[[Subset]]( 1997 .. 2008 > 2005 ) &rarr;? [1, 1, 1]      { Hypothetical only -- error reported }
 
In a case like this, you should use a local index, e.g.:
 
Index I := 1997 .. 2008 do Subset(I > 2005) &rarr; [2006, 2007, 2008]
 
  
However, if you use the ''position:true'' parameter setting, then you can pass an array with an implicit index, since element positions (and not index values) are returned:
+
However, if you use the ''position:true'' parameter setting, then you can pass an array with an [[implicit index]], since element positions (and not index values) are returned:
[[Subset]]( 1997 .. 2008 > 2005, position:true ) &rarr; [10, 11, 12]
 
  
Another example:
+
:<code>Subset(1997..2008 > 2005, position: true) &rarr; [10, 11, 12]</code>
Index Years := 2005 .. 2009
 
Subset(Years < 2008, position:true) &rarr; [1, 2] 
 
Subset(Years > 2005 and Years < 2008, position:true) &rarr; [2, 3]
 
  
 
===I and resultIndex===
 
===I and resultIndex===
Line 50: Line 55:
 
When «i» and «resultIndex» are used in conjunction, the result is an array with fully explicit indexes.   
 
When «i» and «resultIndex» are used in conjunction, the result is an array with fully explicit indexes.   
  
When you are using [[Subset]] to define an index, you should not be using «resultIndex», «d» should be 1-dimensional, and «i» is superfluous.   
+
When you use [[Subset]] to define an index, you should not use «resultIndex», «d» should be 1-dimensional, so «i» is superfluous.   
  
Example:
+
===WarnEmpty===
 +
The optional «warnEmpty» parameter controls whether [[Subset]] issues a warning when no true elements are found in «d».  The default value for «warnEmpty» is false, i.e. it gives no warning.
 +
 
 +
==Details & More Examples==
 +
===Example 1===
 +
Let:
 +
:<code>Index Years := 2005..2009</code>
 +
 +
Then:
 +
:<code>Subset(Years < 2008, position:true) &rarr;</code>
 +
::<code>[1, 2]</code>
 +
:<code>Subset(Years > 2005 and Years < 2008, position: true) &rarr;</code>
 +
::<code>[2, 3]</code>
 +
 
 +
===Example 2===
 +
Let:
 +
:<code>Variable A :=</code>
 
:{| class="wikitable"
 
:{| class="wikitable"
! rowspan=2 colspan=2 | A
+
!
! colspan=5 align=center | I
+
! colspan="5" align="center" | I &#9654;
 
|-
 
|-
 +
!J &#9660;
 
! 1 !! 2 !! 3 !! 4 !! 5
 
! 1 !! 2 !! 3 !! 4 !! 5
 
|-
 
|-
| rowspan=3 | J
 
 
! 1  
 
! 1  
 
| 73 || 23 || 19 || 54 || 12
 
| 73 || 23 || 19 || 54 || 12
Line 70: Line 91:
 
|}
 
|}
  
:Index K := [1,2]
+
:<code>Index K := [1, 2]</code>
  
:{|class="wikitable" style="text-align:center"
+
Then:
| [[Subset]](A>50,i:I,resultIndex:K) &rarr;
+
:<code> Subset(A > 50, i: I, resultIndex: K) &rarr;</code>
|
+
:{| class="wikitable"
{| class="wikitable"
+
!
! rowspan=2 colspan=2 | &nbsp;
+
! colspan="2" |K &#9654;
! colspan=2 align=center | K
 
 
|-
 
|-
! width="50px" | 1 !! width="50px" | 2  
+
!J &#9660;
 +
!1
 +
!2
 
|-
 
|-
| rowspan=3 | J
 
 
! 1  
 
! 1  
 
| 1 || 4
 
| 1 || 4
Line 90: Line 111:
 
! 3  
 
! 3  
 
| 4 || «null»
 
| 4 || «null»
|}
 
 
|}
 
|}
  
Notice that in the '''J=1''' for, there are exactly two elements satisfying the condition, [1, 4].  In the second row there are three elements, [3, 4, 5], but since our result index '''K''' has only two elements, we retain only the first two.  In the third row of '''A''', there is only one cell satisfying the condition, hence the second cell in the result is padded with [[Null]].
+
Notice that in the first row, <code>J = 1</coed>, two elements, <code>K=[1, 4]</code>,  satisfy the condition, <code>A > 50</code>.  In the second row there are three elements, <code>K= [3, 4, 5]</code>, but since our result index '''K''' has only two elements, we retain only the first two.  In the third row of '''A''', there is only one cell satisfying the condition, hence the second cell in the result is padded with [[Null]].
 
 
For [[Subset]] to reliably array abstract, both indexes must be specified, although it is fine to use the same index for both.  Since they are optional, and not the second and third parameter, they should be specified using the named calling convention as illustrated above.
 
 
 
===WarnEmpty===
 
The optional «warnEmpty» parameter controls whether [[Subset]] issues a warning when no true elements are found in «d».  The default value for «warnEmpty» is false, i.e. no warning is given.  
 
  
==History==
+
For [[Subset]] to reliably array abstract, you should specify both indexes, although it is fine to use the same index for both. Since they are optional, use the the named calling convention as illustrated above.
* Unless an explicit test is specified, [[Null]] elements are removed in release 4.2 and later, but retained prior to 4.2.
 
* The optional parameters «position», index «I», «resultIndex» and «warnEmpty» were introduced in Analytica 4.2 and do not work in earlier versions.
 
* In Analytica 4.2 and earlier, «warnEmpty» defaulted to ''true'', in Analytica 4.3 and later it defaults to ''false''.
 
  
 
== See Also ==
 
== See Also ==

Latest revision as of 21:26, 4 August 2016


Subset(d, position, i, resultIndex, warnEmpty)

If «d» is an Index or a 1D array index by «i», Subset(d) returns a list of all the elements of the index for which «d» is true (1). The function is used to create a new index that is a subset of an existing index.

If «d» is not an Index or 1D array -- if it's scalar or has more than 1D -- it returns an error.

Subset in its standard one-parameter usage does not array abstract because it returns an unindexed list.

When there is the possibility of having Null values in your array, you should consider whether you want those elements to be retained. The following two uses respectively retain or remove Null elements:

Subset(Test Or Test = Null)
Subset(Test And Test <> Null)

Without the explicit test, Null elements are removed.

Example

Let:

Index Years := 2005..2009

Then:

Subset(Years < 2008) →
[2005, 2006, 2007]
Subset(Years > 2005 and Years < 2008) →
[2006, 2007]

Optional parameters

Position

The optional «position» parameter specifies whether the index element or the index position should be returned (see Associative vs. Positional Indexing). When «position» is omitted or when position: false is specified, the index element is returned. position: true returns the positions of each non-zero element along «d»’s index for which «d»’s values are true: Subset(D, position: true)

The need to specify positions arises when your index contains duplicate values, or when the input to the function is an array with an implicit index.

In general, the one-dimensional array passed to Subset needs to be explicitly indexed because the index elements corresponding to true elements is returned. Thus, an implicitly indexed array (e.g., a list) also results in an error. Theoretically Subset could return a result in the implicitly indexed case, but it would be misleading since it would just be returning all the true values, for example:

Subset(1997..2008 > 2005) →? [1, 1, 1] { Hypothetical only -- error reported }

In a case like this, you should use a local index, e.g.:

Index I := 1997..2008 do Subset(I > 2005) → [2006, 2007, 2008]

However, if you use the position:true parameter setting, then you can pass an array with an implicit index, since element positions (and not index values) are returned:

Subset(1997..2008 > 2005, position: true) → [10, 11, 12]

I and resultIndex

The basic use of Subset does not allow d to contain more than one dimension, and will not evaluate or array abstract otherwise (since an unindexed list is returned). If you have a two- (or more-) dimensional array or are using Subset in calculations that are potentially array-based, you'll need to identify one of those dimensions as the one that the subset is being taken over. For each slice along the other dimensions, a Subset can then be separately computed, and each of those subsets may have a different number of elements.

The optional index parameter «i» together with «resultIndex» allow Subset to be used in a manner that fully array abstracts: «i» specifies which dimension to operate over, and «resultIndex» specifies the index of the result. For the individual subsets that contain more elements than the «resultIndex», only the first Size(«resultIndex») members of the subset are retained. For those subsets that are smaller than the «resultIndex», the remaining entries in the result are padded with Null values.

When «i» and «resultIndex» are used in conjunction, the result is an array with fully explicit indexes.

When you use Subset to define an index, you should not use «resultIndex», «d» should be 1-dimensional, so «i» is superfluous.

WarnEmpty

The optional «warnEmpty» parameter controls whether Subset issues a warning when no true elements are found in «d». The default value for «warnEmpty» is false, i.e. it gives no warning.

Details & More Examples

Example 1

Let:

Index Years := 2005..2009

Then:

Subset(Years < 2008, position:true) →
[1, 2]
Subset(Years > 2005 and Years < 2008, position: true) →
[2, 3]

Example 2

Let:

Variable A :=
I ▶
J ▼ 1 2 3 4 5
1 73 23 19 54 12
2 11 1 98 76 52
3 22 33 49 87 22
Index K := [1, 2]

Then:

Subset(A > 50, i: I, resultIndex: K) →
K ▶
J ▼ 1 2
1 1 4
2 3 4
3 4 «null»

Notice that in the first row, J = 1</coed>, two elements, K=[1, 4], satisfy the condition, A > 50. In the second row there are three elements, K= [3, 4, 5], but since our result index K has only two elements, we retain only the first two. In the third row of A, there is only one cell satisfying the condition, hence the second cell in the result is padded with Null.

For Subset to reliably array abstract, you should specify both indexes, although it is fine to use the same index for both. Since they are optional, use the the named calling convention as illustrated above.

See Also

Comments


You are not allowed to post comments.