Announcement

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

  • How to STATA matrix sort by column name

    How can I convert A to B?

    A)
    . matrix list A

    A[5,5]
    c1 c2 c4 c5 c3
    r1 12 2 0 9 0
    r2 3 2 0 2 0
    r3 2 0 1 3 0
    r4 7 2 0 8 0
    r5 8 1 0 7 0


    B)
    c1 c2 c3 c4 c5
    r1 12 2 0 0 9
    r2 3 2 0 0 2
    r3 2 0 0 1 3
    r4 7 2 0 0 8
    r5 8 1 0 0 7




    Click image for larger version

Name:	20220106_170233.png
Views:	1
Size:	244.5 KB
ID:	1643806



    Last edited by Johnkim Kim; 06 Jan 2022, 02:09.

  • #2
    See https://www.statalist.org/forums/for...ne-of-its-rows

    Also see

    Code:
    help matrix
    for simple matrix operations which can help. In #1, e.g.,

    Code:
    mat A=(12, 2, 0, 9, 0)\(3, 2, 0, 2, 0)\(2, 0, 1, 3, 0)\(7, 2, 0, 8, 0)\(8, 1, 0, 7, 0)
    mat wanted= A[1...,1..2], A[1..., 5], A[1..., 3..4]
    *SPECIFYING COLUMN NAMES
    mat wanted2= A[1...,"c1".."c2"], A[1..., "c5"], A[1..., "c3".."c4"]
    mat l A
    mat l wanted
    mat l wanted2
    Res.:

    Code:
    . mat l A
    
    A[5,5]
        c1  c2  c3  c4  c5
    r1  12   2   0   9   0
    r2   3   2   0   2   0
    r3   2   0   1   3   0
    r4   7   2   0   8   0
    r5   8   1   0   7   0
    
    . 
    . mat l wanted
    
    wanted[5,5]
        c1  c2  c5  c3  c4
    r1  12   2   0   0   9
    r2   3   2   0   0   2
    r3   2   0   0   1   3
    r4   7   2   0   0   8
    r5   8   1   0   0   7
    
    . 
    . mat l wanted2
    
    wanted2[5,5]
        c1  c2  c5  c3  c4
    r1  12   2   0   0   9
    r2   3   2   0   0   2
    r3   2   0   0   1   3
    r4   7   2   0   0   8
    r5   8   1   0   0   7
    
    .
    Last edited by Andrew Musau; 06 Jan 2022, 02:44.

    Comment


    • #3
      This is exactly what I want. Thank you very much.

      Comment

      Working...
      X