Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • extracting column number based on minimum row value

    Hello
    I have a sample data set with four columns (L1 to L4) and five rows. I am trying to extract the column number which contains the minimum value across the rows. If there are two rows with the same minimum value, I would like to extract the first column that contains the minimum value (ignoring the other columns).
    Code:
    . version
    version 13.1
    
    . clist L1-L4
    
                L1         L2         L3         L4
      1.         2          2          .          .
      2.         3          2          .          2
      3.         7          3          6          8
      4.         4          3          7          3
      5.         4          5          7          9


    I then calculate the row minimum.

    Code:
    . egen min=rowmin(L1 L2  L3  L4)
    
    .clist
    
                L1         L2         L3         L4        min
      1.         2          2          .          .          2
      2.         3          2          .          2          2
      3.         7          3          6          8          3
      4.         4          3          7          3          3
      5.         4          5          7          9          4
    Now I would like to extract the column number which contains the minimum value. If there are two minimum values in a row, the column containing the first minimum value is what I am after,
    I tried the following:

    Code:
     
    
    . gen col_no=.
    (5 missing values generated)
    
    . local array1 "L1 L2 L3 L4" 
    
    . local i=1
    
    . local n : word count `array1'
    
    . while `i' <= `n' {
      2.          local var1 : word `i' of `array1'
      3.                  replace col_no =`i' if `var1'==min
      4.                  local i = `i' + 1
      5.         }               
    (2 real changes made)
    (4 real changes made)
    (0 real changes made)
    (2 real changes made)
    
    . clist
    
                L1         L2         L3         L4        min     col_no
      1.         2          2          .          .          2          2
      2.         3          2          .          2          2          4
      3.         7          3          6          8          3          2
      4.         4          3          7          3          3          4
      5.         4          5          7          9          4          1
    The output is not exactly what I wanted. For rows 1, 2, and 4, the extracted column numbers should be 1, 2 and 2 respectively.

    Thanks in advance.



  • #2
    I cannot import your data and try it, but something like this should do the trick:


    Code:
    gen col_no = 1 if L1==min
    replace col_no = 2 if L2==min & missing(col_no)
    replace col_no= 3 if L3==min & missing(col_no)
    replace col_no=4 if L4==min & missing(col_no)

    Comment


    • #3
      Thanks Joro. This solves the problem I have.

      Comment

      Working...
      X