Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 66
4.3.4.3 Access Subsets of a Matrix
ОглавлениеIt might be obvious, that we can access one element of a matrix by using the row and column number. That is not all, R has a very flexible – but logical – model implemented. Let us consider a few examples that speak for themselves.
M <- matrix(c(10:21), nrow = 4, byrow = TRUE) M ## [,1] [,2] [,3] ## [1,] 10 11 12 ## [2,] 13 14 15 ## [3,] 16 17 18 ## [4,] 19 20 21 # Access one element: M[1,2] ## [1] 11 # The second row: M[2,] ## [1] 13 14 15 # The second column: M[,2] ## [1] 11 14 17 20 # Row 1 and 3 only: M[c(1, 3),] ## [,1] [,2] [,3] ## [1,] 10 11 12 ## [2,] 16 17 18 # Row 2 to 3 with column 3 to 1 M[2:3, 3:1] ## [,1] [,2] [,3] ## [1,] 15 14 13 ## [2,] 18 17 16