# Chapter 6

## Indexing

What is example where example is

`example <- c("cat", "dog", "pelican", "frog")`
cat

because the index, i is given the value of 1, and "cat" is the first value in example.

What is example where example is

`example <- c("cat", "dog", "pelican", "frog")`

NA

because the index, i is given the value of 5, and there are only four values in example.

What is example[1, 3] where example is

`example <- c("cat", "dog", "pelican", "frog")`

nothing

because an index can only accept one argument.

What is example[c(1, 3)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "pelican"

because the single argument, c(1, 3) is a vector of indices corresponding to the first and third values, which are "cat" and "pelican".

What is example[c(1, 1)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "cat"

because the single argument, c(1, 1) is a vector of indices corresponding to the first value twice, and the first value is "cat".

What is "cat"

 "cat"

because the single argument, 1 is a vector of length one corresponding to the first value of the unnamed vector of length one, and that first value is "cat".

What is example[-2] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "pelican" "frog"

because the second value of example is "dog", and "cat", "pelican" and "frog" are the remaining values.

What is example[c(-1, -3)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "dog" "frog"

because the first and third values of example are "cat" and "pelican", and "dog" and "frog" are the remaining values.

What is example[-c(1, 3)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "dog" "frog"

because the first and third values of example are "cat" and "pelican", and "dog" and "frog" are the remaining values.

What is example[-c(1, 1)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "dog" "pelican" "frog"

because the single argument, c(1, 1) is a vector of indices corresponding to the first value twice, and the first value is "cat", while "dog", "pelican" and "frog" are the remaining values.

What is example[c(-1, 2)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

nothing

because the first index, -1 is negative while the second index, 2 is positive.

What is example[c(0)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

character(0), an empty vector

because there is nothing at index zero in a vector.

What is example[c(TRUE, FALSE, TRUE, FALSE)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "pelican"

because in the indexing vector, the first and third positions were TRUE, and "cat" and "pelican" are in the first and third position in example.

What is example[c(TRUE, FALSE)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "pelican"

because in the indexing vector, the first position is TRUE, the second position is FALSE and the indexing vector is recycled to fit the length of the vector being indexed.

What is example[c(TRUE)] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "dog" "pelican" "frog"

because in the indexing vector, the first position is TRUE, and the indexing vector is recycled to fit the length of the vector being indexed.

What is example[TRUE] where example is

`example <- c("cat", "dog", "pelican", "frog")`

 "cat" "dog" "pelican" "frog"

because the index is interpreted as a vector with one logical value, TRUE. In the indexing vector, the first position is TRUE, and the indexing vector is recycled to fit the length of the vector being indexed.

What is numbers == 5 where numbers is

`c(2:10)`

 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers Only the statement 5 == 5 is TRUE, while all others are replaced by FALSE.

What is numbers[numbers == 5] where numbers is

`c(2:10)`

 5

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers, and returns those that are TRUE. Only the statement 5 == 5 is TRUE, so only 5 is returned.

What is numbers[numbers > 8] where numbers is

`c(2:10)`

 9 10

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers, and returns those that are TRUE. The statements 9 > 8 and 10 > 8 are TRUE, so 9 and 10 are returned.

What is !numbers > 4 where numbers is

`c(2:10)`

 TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers. Because the expression is prefaced by ! it assigns TRUE when elements evalute to FALSE and vice versa.

What is numbers[!numbers > 4] where numbers is

`c(2:10)`

 2 3 4

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers. Because the expression is prefaced by ! it assigns TRUE when elements evalute to FALSE and vice versa. The statements 2 > 4, 3 > 4 and 4 > 4 are FALSE, so they are assigned to TRUE so 2, 3 and 4 are returned.

What is numbers > 4 & numbers == 6 where numbers is

`c(2:10)`

 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers. The logical expression is a compound expression using & which means that expressions must evaluate to true for the corresponding index element to be TRUE. Because only 6 is both greater than four and equal to six, all other elements of the index vector are FALSE.

What is numbers[numbers > 4 & numbers == 6] where numbers is

`c(2:10)`

 6

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers. The logical expression is a compound expression using & which means that expressions must evaluate to true for the corresponding index element to be TRUE. Because only 6 is both greater than four and equal to six, all other elements of the index vector are FALSE. The TRUE value, 6, is returned.

What is numbers > 8 | numbers < 3 where numbers is

`c(2:10)`

 TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers. The logical expression is a compound expression using | which means that expressions only need to evaluate to true for one of the two expressions, not both, for the corresponding index element to be TRUE. Because 9 and 10 are greater than eight, and 2 is less than three, these elements are set to TRUE, while all others are FALSE.

What is numbers[numbers > 8 | numbers < 3] where numbers is

`c(2:10)`

 2 9 10

because the logical expression generates an index of TRUE and FALSE values corresponding to the elements of numbers. The logical expression is a compound expression using | which means that expressions only need to evaluate to true for one of the two expressions, not both, for the corresponding index element to be TRUE. Because 9 and 10 are greater than eight, and 2 is less than three, these elements are set to TRUE, while all others are FALSE. The TRUE values, 2, 9 and 10 are returned.

What is example["pet three"] where

```example <- c("cat", "dog", "pelican", "frog")
names(example) <- c("pet one", "pet two", "pet three", "pet four")```

"pelican"

because the index is a named value, and in the vector being indexed into, the value named "pet three" is "pelican".

What is example[c("pet three", "pet four")] where

```example <- c("cat", "dog", "pelican", "frog")
names(example) <- c("pet one", "pet two", "pet three", "pet four")```

"pelican" "frog"

because the index is two named values, and in the vector being indexed into, the value named "pet three" and "pet four" are "pelican" and "frog".

What is example[length(example)] where

`example <- c("cat", "dog", "pelican", "frog")`

 "frog"

because the index is equal to length(example), and the length of example is four, and the fourth value in example is frog.

What is example[exampleIndex] where

```example <- c("cat", "dog", "pelican", "frog")
exampleIndex <- c(length(example), length(example), length(example))```

 "frog" "frog" "frog"

because the index contains length(example) three times, and the length of example is four, and the fourth value in example is frog.

Summary: Vector Indexing

You can index into a vector using square brackets ([]). Indexes can be a vector of values, or single values (which are interpretable as vectors of length one). When positively signed values are used, the positions indicated by the vector will be returned. When negatively signed values are used, all positions except those indicated by the vector will be returned. You cannot mix positively and negatively signed integers. In addition to integers, you can also use logical values as an index. When doing so, positions selected with TRUE will be returned. You can use characters in the index as well, if those characters correspond to names in a named vector.

Vector indexing is subject to recycling. If your vector index is shorter than the vector being indexed, R will start from the beginning of your vector index, looping through it as many times as needed to match the length of the vector being indexed.

What is flowers where

`flowers <- matrix(c("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "daisy"

because the index is given the value of 1, and "daisy" is the first value in flowers

What is flowers where

`flowers <- matrix(c("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "goldenrod"

because the index is given the value of 2, and "goldenrod" is the second value in the first column of flowers:

```     [,1]        [,2]
[1,] "daisy"     "sunflower"
[2,] "goldenrod" "dandelion"
```

What is flowers where

`flowers <- matrix(c("daisy", "goldenrod", "sunflower", "dandelion"), 2, byrow=TRUE)`

 "sunflower"

because the index is given the value of 2, and "sunflower" is the second value in the first column of flowers:

```     [,1]        [,2]
[1,] "daisy"     "goldenrod"
[2,] "sunflower" "dandelion"
```

What is flowers[2,] where

`flowers <- matrix(c("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "goldenrod" "dandelion"

because the index given specifies the row but not the column, and the second row in flowers is  "goldenrod" "dandelion"

What is flowers[,2] where

`flowers <- matrix(c("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "sunflower" "dandelion"

because the index given specifies the column but not the row, and the second column in flowers is  "sunflower" "dandelion"

What is class(flowers) where

`flowers <- matrix(c("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "character"

because the matrix flowers was created from a character vector, and we are asking about a single element of that matrix.

What is class(flowers) where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "list"

because the matrix flowers was created from a list vector, and we are asking about a single element of that matrix.

What is class(flowers[]) where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "character"

because the matrix flowers was created from a list vector, and we are asking about a single element within a single element of that matrix.

What is class(flowers[]) where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "character"

because the matrix flowers was created from a list vector, and we are asking about a single element within a single element of that matrix.

What is flowers[2,2] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "dandelion"

because the index given specifies second row and the second column, and the value in the second row and second column in flowers is "dandelion".

What is flowers[2,2] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "dandelion"

because the index given specifies second row and the second column, and the value in the second row and second column in flowers is "dandelion".

What is flowers[-1] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "goldenrod" "sunflower" "dandelion"

because "daisy" is the first element in the matrix, and "goldenrod", "sunflower" and "dandelion" are the remaining elements.

What is flowers[-1,] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

[2,] "goldenrod" "dandelion"

because [1,] "daisy" "sunflower" is the first row in the matrix, and "goldenrod", and [2,] "goldenrod" "dandelion" are the remaining rows.

What is flowers[,-2] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "daisy" "goldenrod"

because  "sunflower" "dandelion" is the second column in the matrix, and "goldenrod", and  "daisy" "goldenrod" are the remaining columns.

What is flowers[1, 2] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "sunflower"

because the two arguments, 1 and two, specify the value to be returned, and the value in the first row, second column is "sunflower".

What is flowers[c(1, 2)] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "daisy" "goldenrod"

because the single argument, c(1, 2) is a vector of indices corresponding to the first and second values, which are "daisy" and "goldenrod".

What is flowers[c(-3)] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "daisy" "goldenrod" "dandelion"

because the third value of flowers is "sunflower", and "daisy", "goldenrod" and "dandelion" are the remaining values.

What is flowers[c(TRUE,FALSE)] where

`flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)`

 "daisy" "sunflower"

because in the indexing vector, the first position is TRUE, the second position is FALSE and the indexing vector is recycled to fit the length of the vector being indexed.

What is flowers[matrix(c(1,4))] where

```flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)
colnames(flowers) <- c("pretty", "ugly")
rownames(flowers) <- c("lots", "few")```

 "daisy" "dandelion"

because the single argument, matrix(c(1, 4)) is a vector of indices corresponding to the first and fourth values, which are "daisy" and "dandelion".

What is flowers["lots", "pretty"] where

```flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)
colnames(flowers) <- c("pretty", "ugly")
rownames(flowers) <- c("lots", "few")```

 "daisy"

because "daisy" is the value of the element in row "lots" and column "pretty".

What is flowers["ugly", "few"] where

```flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)
colnames(flowers) <- c("pretty", "ugly")
rownames(flowers) <- c("lots", "few")```

nothing

because there is no row "ugly" or column "few" in matrix flowers.

What is flowers[c("lots", "pretty")] where

```flowers <- matrix(list("daisy", "goldenrod", "sunflower", "dandelion"), 2)
colnames(flowers) <- c("pretty", "ugly")
rownames(flowers) <- c("lots", "few")```

 NA NA

Summary: Matrix Indexing

As with vector indexing, you can index into a matrix using square brackets [] or double square brackets [[ ]]. Generally, double square brackets are used when the matrix is composed of lists. Using square brackets then returns the single values inside each list instead of the single-value list itself. For matrices, indexes can be one or two individual values, vectors of values, or matrices of values.

When positively signed values are used, the positions indicated by the values will be returned. When negatively signed values are used, all positions except those indicated by the values will be returned. You cannot mix positively and negatively signed integers. In addition to integers, you can also use logical values as an index. When doing so, positions selected with TRUE will be returned. You can use characters in the index as well, if those characters correspond to rownames or column names in the matrix being indexed into.

Matrix indexing is subject to recycling.

What is myArray[myIndex] where

```myArray <- array(1:6, dim=c(2,2,2))
myIndex <- array(1, dim=c(2,2,2))```

 1 1 1 1 1 1 1 1

because myIndex is an array of 1s in 2 x 2 x 2 dimenions, and the value of the first position in myArray is 1.

What is myArray[myIndex] where

```myArray <- array(1:6, dim=c(2,2,2))
myIndex <- array(7, dim=c(2,2,2))```

 1 1 1 1 1 1 1 1

because myIndex is an array of 1s in 2 x 2 x 2 dimenions, and the value of the seventh position in myArray is 1.

What is myArray[myIndex] where

```myArray <- array(1:6, dim=c(2,2,2))
myIndex <- array(c(1,2,7), dim=c(2,2,2))```

 1 2 1 1 2 1 1 2

because myIndex is an array of 1s in 2 x 2 x 2 dimenions, and the value of the first, second and seventh positions of myArray are 1, 2 and 7 respectively.

What is myArray[myIndex] where

```myArray <- array(1:6, dim=c(2,2,2))
myIndex <- array(c(1,2,7), dim=c(2,2,2))```

 1 2 1 1 2 1 1 2

because myIndex is an array of 1s in 2 x 2 x 2 dimenions, and the value of the first, second and seventh positions of myArray are 1, 2 and 7 respectively.

What is myArray[myIndex] where

```myArray <- array(1:6, dim=c(2,2,2))
myIndex <- c(1,2,3,7)```

 1 2 3 1

because myIndex is a vector of values 1, 2, 3 and 7 and the values of those positions in myArray are 1, 2, 3 and 1.

Summary: Array Indexing

Three-dimensional array indexing is very similar to two-dimensional array indexing, aka matrix indexing. As with matrix and vector indexing, you can do the following:

• index using square brackets [] or double square brackets [[]]
• use individual values, vectors of values, matrices of values, or arrays of values
• use positive or negative values but not both at the same time
• use logical or character values