[]
Square bracket operatorThe square bracket operator []
is used to select individual rows, columns or elements from a table. It is similar to array access in other languages, but more versatile with a greater variety of access methods. The square bracket operator can also be used to select elements from open region (an open region consists of one coordinate that specifies the upper left corner). In the case of an open region selecting an entire row or column is not possible because they will have infinite size.
Selectors can be integer (showing corresponding row or column), boolean (True in row or column means select that row or column) or a mixture of the two.
[]
operator syntaxThe []
operator recognises two different calling styles depending on if selection by location or selection by value is required.
The syntax for a selection by location is:
region[ *row selector*, *column selector*]
When region
is a table or fully defined region, row selector
and column selector
are optional, but one must appear. The comma between them is not optional. It is used to disambiguate calls with a single parameter, as will be demonstrated below. Using open regions with []
is discussed later.
The syntax for selection by value is:
region[ *key* ]
It looks up key
in the first column of region
. This is explained in more detail below.
[]
To select a single row simply pass in its index as the row selector
. Note the comma:
#a[1,]
To select multiple rows the row selector
must be a column vector. Below an inline column vector built with the vertical table concatenation operator \\\\
is used. The values in the column vector specify which rows to select and which order to return them. You can use this to re-order tables. Note again the trailing comma marking an empty column selector
.