Matrix subsetting
Just like vectors, matrices can be selected from and subsetted! To do this, you will again use [ ], but this time it will have two inputs. The basic structure is:
my_matrix[row, col]
Then:
To select the first row and first column of stocks from the last example: stocks[1,1]
To select the entire first row, leave the col empty: stocks[1, ]
To select the first two rows: stocks[1:2, ] or stocks[c(1,2), ]
To select an entire column, leave the row empty: stocks[, 1]
You can also select an entire column by name: stocks[, "apple"]
Cet exercice fait partie du cours
Introduction to R for Finance
Instructions
stocksis in your workspace.- Select the third row of
stocks. - Select the fourth and fifth row of the
ibmcolumn ofstocks. - Select the
appleandmicrcolumns fromstocksusingc()inside the brackets.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Third row
# Fourth and fifth row of the ibm column
# apple and micr columns