Search code examples
matrixstata

Access element of matrix by value


Let's say I define a matrix:

 matrix a = (2,3 \ 4,7 \ 6,13)

I can access "13" like this:

display a[3,2]

Is it also possible to access "13" while referring to "6" to specify the row? In other words, we would somehow signify that the row is the row (there could be more than one) that contains a 6 in the first column and then we want the second column of this row.

In R, we might do it like this:

a1 <- data.frame(c(2,4,6), c(3,7,13))
a1[a1[,1]==6, 2]

Is there anything analogous in Stata?


Solution

  • You could do this with Stata's matrix language, with some programming, but I would turn to Mata whose defined functions allow direct solutions similar in spirit to R. Consider this dialogue.

    . mata
    ------------------------------------------------- mata (type end to exit) --------------
    : a = (2,3 \ 4,7 \ 6,13)
    
    : a :== 1
           1   2
        +---------+
      1 |  0   0  |
      2 |  0   0  |
      3 |  0   0  |
        +---------+
    
    : a :== 6
           1   2
        +---------+
      1 |  0   0  |
      2 |  0   0  |
      3 |  1   0  |
        +---------+
    
    : rowsum(a :== 6)
           1
        +-----+
      1 |  0  |
      2 |  0  |
      3 |  1  |
        +-----+
    
    : select(a, rowsum(a :== 6))
            1    2
        +-----------+
      1 |   6   13  |
        +-----------+
    
    : a2 = select(a, rowsum(a :== 6))
    
    : a2[, 2]
      13
    
    :  b = (6,6 \ 6,6 \ 6,6)
    
    : select(b, rowsum(b :== 6))
           1   2
        +---------+
      1 |  6   6  |
      2 |  6   6  |
      3 |  6   6  |
        +---------+
    
    : b2 = select(b, rowsum(b :== 6))
    
    : b2[, 2]
           1
        +-----+
      1 |  6  |
      2 |  6  |
      3 |  6  |
        +-----+
    

    "row contains a 6" is defined by the total of "element is equal to 6" across rows. Note that the code works if (a) there is more than one 6 on a row and/or (b) there is more than one row with a 6. In the last case, what is selected contains more than one element.

    The notation should seem self-explanatory, except possibly that : as a prefix signals "elementwise" operations. To copy a Stata matrix into Mata, use st_matrix().

    Note: Working out what the code should be to select in the first column only is set as an exercise for the zealous.