4.3 Indexing and Subsetting

Subsetting works by using indices of interest with square brackets.

In matrix, using a single index treats it as a vector and selects the cells based on their order

matrix_data2
##            Thea Pravin Troy Albin Clementine
## Thea          1      1    1     1          0
## Pravin        1      0    1     1          1
## Troy          0      0    1     0          1
## Albin         1      1    0     1          0
## Clementine    1      0    0     1          0
matrix_data2[c(1,7,20,25)]
## [1] 1 0 1 0

Using two indices subsets the matrix based on [row,column] pairs

matrix_data2[1,4]
## [1] 1
matrix_data2[c(1,2),c(4,5)]
##        Albin Clementine
## Thea       1          0
## Pravin     1          1

We can also subset based on the column/row names

matrix_data2["Thea", "Troy"]
## [1] 1

We can use subsetting to change specific cells in the matrix

matrix_data2[c(1,7,20,25)] <- 7
matrix_data2
##            Thea Pravin Troy Albin Clementine
## Thea          7      1    1     1          0
## Pravin        1      7    1     1          1
## Troy          0      0    1     0          1
## Albin         1      1    0     1          0
## Clementine    1      0    0     7          7
matrix_data2[c(1,2),c(4,5)] <- 9
matrix_data2
##            Thea Pravin Troy Albin Clementine
## Thea          7      1    1     9          9
## Pravin        1      7    1     9          9
## Troy          0      0    1     0          1
## Albin         1      1    0     1          0
## Clementine    1      0    0     7          7

Subsetting in dataframe using a single index works differently.

#select columns 1, 2, and 3
df_data2[c(1,2,3)]
##            Thea Pravin Troy
## Thea          1      1    1
## Pravin        1      0    1
## Troy          0      0    1
## Albin         1      1    0
## Clementine    1      0    0

Finally, we may wish to remove columns in a data.frame, matrix or vector. We can use the subset function to do this.

#
(subset( vector_123, vector_123 > 1))
## [1] 2 3
(vector_123[vector_123>1])
## [1] 2 3