Sorts the levels of a factor according to an index vector (R.W. Payne).
Options
DIRECTION = string token |
Direction in which to sort the index (ascending , descending ); default asce |
---|---|
SETATTRIBUTES = string tokens |
Which aspects of each NEWFACTOR to define (levels , labels , values ); default * i.e. labels and values if defined for FACTOR , also levels if not the integers 1,2… |
Parameters
FACTOR = factors |
Factors whose levels are to be reordered |
---|---|
INDEX = variate, text or one-way table |
Index vectors defining the ordering of the levels of each factor |
NEWFACTOR = factors |
New factors with reordered levels; if unset, the original FACTOR is redefined |
NEWLEVELS = variates |
Saves the (reordered) levels as defined for each NEWFACTOR |
Description
This procedure reorders the levels of a factor. The factor is specified by the FACTOR
parameter. The NEWFACTOR
parameter can specify the identifier for the new reordered factor (so that FACTOR
is left unchanged). If this is not supplied, the original FACTOR
is redefined with its levels in the new order.
The order is defined by an index vector, specified by the INDEX
parameter. This can be a variate, or a text, or a one-way table, whose number of values is equal to the number of levels of the factor. The levels are thus sorted in parallel with the INDEX
(using the SORT
directive), and the DIRECTION
option indicates whether this is to be into ascending or descending order.
The SETATTRIBUTES
option specifies which of the labels, levels and values to define for the NEWFACTOR
. If SETATTRIBUTES
is not set, the default is to define whichever of these has been defined for the FACTOR
. In particular, note that levels are then not defined if the FACTOR
levels are simply 1,2…
The NEWLEVELS
parameter can specify a variate to save the levels of the NEWFACTOR
. You can use this as the setting of the OLDPOSITIONS
and NEWPOSITIONS
parameters of the COMBINE
directive in order to reorder tables classified by the factor. Or, you can discover the (ordinal) number of the original level corresponding to each new level by
GETATTRIBUTE [ATTRIBUTE=levels] FACTOR; SAVE=FacLev
CALCULATE OldLevelNumber\
= POSITION(NEWLEVELS; FacLev['levels'])
Options: DIRECTION
, SETATTRIBUTES
.
Parameters: FACTOR
, INDEX
, NEWFACTOR
, NEWLEVELS
.
Method
FACSORT
uses the standard Genstat manipulation commands, such as SORT
.
Action with RESTRICT
Any restrictions are ignored.
See also
Procedures: FACAMEND
, FACEXCLUDEUNUSED
, FACLEVSTANDARDIZE
, FACUNIQUE
, FDISTINCTFACTORS
.
Commands for: Calculations and manipulation.
Example
CAPTION 'FACSORT example',\ 'Data from Snedecor & Cochran (1980), Statistical Methods',\ '(seventh edition) p. 305.';\ STYLE=meta,plain FACTOR [NVALUES=60; LABELS=!T(Beef,Cereal,Pork); VALUES=(1...3)20] Source FACTOR [NVALUES=60; LABELS=!T(High,Low); VALUES=3(1,2)10] Amount VARIATE [NVALUES=60] Gain READ Gain 73 98 94 90 107 49 102 74 79 76 95 82 118 56 96 90 97 73 104 111 98 64 80 86 81 95 102 86 98 81 107 88 102 51 74 97 100 82 108 72 74 106 87 77 91 90 67 70 117 86 120 95 89 61 111 92 105 78 58 82 : TREATMENTS Source*Amount ANOVA [PRINT=means; FPROBABILITY=yes] Gain AKEEP Source*Amount; MEANS=SourceMeans,AmountMeans,SourceByAmountMeans FACSORT [SETATTRIBUTE=levels,labels] Source; INDEX=SourceMeans;\ NEWFACTOR=Sources; NEWLEVELS=SourceLev & Amount; INDEX=AmountMeans; NEWFACTOR=Amounts; NEWLEVELS=AmountLev COMBINE [OLDSTRUCTURE=SourceMeans; NEWSTRUCTURE=SortedSourceMeans]\ OLDDIMENSION=Source; NEWDIMENSION=Sources;\ OLDPOSITIONS=SourceLev; NEWPOSITIONS=SourceLev PRINT SourceMeans,SortedSourceMeans COMBINE [OLDSTRUCTURE=AmountMeans; NEWSTRUCTURE=SortedAmountMeans]\ OLDDIMENSION=Amount; NEWDIMENSION=Amounts;\ OLDPOSITIONS=AmountLev; NEWPOSITIONS=AmountLev PRINT AmountMeans,SortedAmountMeans COMBINE [OLDSTRUCTURE=SourceByAmountMeans;\ NEWSTRUCTURE=SortedSourceByAmountMeans]\ OLDDIMENSION=Amount,Source; NEWDIMENSION=Amounts,Sources; OLDPOSITIONS=AmountLev,SourceLev; NEWPOSITIONS=AmountLev,SourceLev PRINT SourceByAmountMeans,SortedSourceByAmountMeans