[] Square bracket operator

The 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 syntax

The [] operator recognises two different calling styles depending on if selection by location or selection by value is required.

Selecting by location

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.

Selecting by value

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.

Selecting rows with []

To select a single row simply pass in its index as the row selector. Note the comma:

#a[1,]

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/9c443ce4-0057-492e-97f4-f0c5c0a55e76/Screenshot_2021-06-22_at_09.18.50.png

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.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/415bae79-67fd-4ee9-b730-9a25a62ebf4b/Screenshot_2021-06-22_at_09.20.44.png