Indexing
Inhaltsverzeichnis
Indexing with Positive Integers
- there are circumstances where we want to select only some of the elements of a vector/array/dataframe/list
- this selection is done using subscripts (also known as indices)
- subscripts have square brackets [2] while functions have round brackets (2)
- Subscripts on vectors, matrices, arrays and dataframes have one set of square brackets [6], [3,4] or [2,3,2,1]
when a subscript appears as a blank it is understood to mean all of thus
- [,4] means all rows in column 4 of an object
- [2,] means all columns in row 2 of an object.
- subscripts on lists have (usually) double square brackets [ [2] ] or [ [i,j] ]
A vector of positive integers as index:The index vector can be of any length and the result is of the same length as the index vector. For example,
A logical vector as index: Values corresponding to T values in the index vector are selected and those corresponding to F or NA are omitted. For example,
creates a vector without missing values. Also
replaces the missing value by zeros. A common operation is to select rows or columns of data frame that meet some criteria. For example, to select those rows of painters data frame with Colour >= 17:
We may want to select on more than one criterion. We can combine logical indices by the 'and', 'or' and 'not' operators. For example,
List of Logical Operations
Operation |
Description |
! |
logical NOT |
¦ | logical OR |
|
< |
less than |
<= |
less than or equal to |
> |
greater than |
>= |
greater than or equal to |
== |
logical equals (double =) |
!= |
not equal |
¦¦ |
OR with IF |
xor(x,y) |
exclusive OR |
isTRUE(x) |
an abbreviation of identical(TRUE,x) |
If we want to select a subgroup, for example those with schools A, B, and D. We can generate a logical vector using the <latex> \mathtt{\%in\%}</latex> operator as follows:
Sometimes we are interested in the indices of rows satisfying a certain condition. To extract these indices we use the which() command.
Indexing with Character Vectors
A vector character strings with variable names can be used to extract those variables relevant for analysis. This is very useful when we have a large number of variables and we need to work with a few ones. For example,
a vector of character strings could a index on a vector when the vector has names:
Trimming Vectors Using Negative Indices
- an extremely useful facility is to use negative indices to drop terms from a vector
- suppose we wanted a new vector, z, to contain everything but the first element of x