Search code examples
rindexingsyntaxnotation

Matrix indexing notation syntax


Lets create matrix m.

m <- matrix(1:9, 3,3, T); m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9

m[3,1]    # 7
m[3][1]   # 7

Why does second indexing notation work? Is there difference between these two notations? Is it safe to use?

But sequences behave differently:

m[1:2, 1:2]    # works as expected, return matrix
m[1:2][1:2]    # return vector 1 4, why?

Solution

  • A matrix is a vector with dim attributes. By doing the m[3], it returns only the 3rd element. If we want to use a chained extract, then extract the row with column index blank after the , (drop = FALSE - in case we want to avoid coercing the matrix to vector) and select the first element which is the first column

    m[3,, drop = FALSE][1]
    #[1] 7
    

    In the OP's first option, it uses the row index and column index with 3, 1 which selects the element based on both index


    In the updated example, OP specified row index as first 2 rows and columns as first 2 columns. So, it returns a matrix omitting the 3rd row and 3rd column

    m[1:2, 1:2]
    #     [,1] [,2]
    #[1,]    1    2
    #[2,]    4    5
    

    But, in the second case

    m[1:2]
    #[1] 1 4
    

    extracts the first two elements

    likewise, if we do

    m[1:5]
    #[1] 1 4 7 2 5
    

    is the first five elements following the columnwise order

    Therefore,

    m[1:2][1:2]  
    

    returns only 1, 4 because from the first Extract, it is only extracting 1 and 4. Then, the second extract is based on that subset and it also have 2 elements. If we increase the index, those positions are not available and filled by NA

    m[1:2][1:4]
    #[1]  1  4 NA NA
    

    The elementwise indexing is acting on the vector

    c(m)
    #[1] 1 4 7 2 5 8 3 6 9
    

    where the first two elements are 1 and 4