Announcement

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

  • Renaming rows in Matrix

    Dear All,
    I just obtained the following matrix
    Code:
    X[18,4]
             imeu0      imeu1      imeu2      imeu3
     r1  .17012477  .14678881  .14600933  .30892292
     r2  .16162819  .13678399  .14205745  .27877864
     r3  .17284968   .1417264  .14267236  .30505028
     r4  .15048751  .13105537  .13583121  .29497504
     r5  .16008531  .13823907  .14575586  .31288761
     r6  .16042244  .13621108  .15766013  .32482761
     r7  .17476352  .14296345  .15441483  .33454451
     r8  .19413681  .14554305  .15211919  .33655742
     r9  .21324582  .15600634  .15217966  .34234405
    r10  .22980724  .17459309  .16466963  .39787501
    r11  .24465741  .16848315  .17576084  .43677589
    r12  .27512023  .18187349  .18600267  .46879619
    r13  .28697997  .19372031  .20313573  .50782186
    r14  .29941869  .20602649   .2135499  .54286134
    r15   .2944608  .21142302   .2167595  .54684091
    r16    .363224  .21042196  .21408814  .56670946
    r17  .37690601  .22233553  .21801062  .62060088
    r18  .37223187  .23511408  .23473491  .57701015
    How do I rename the rows r1, r2, ..., r18 to 1990, 1991,..., 2007?

    Thanks,
    Dapel

  • #2
    Code:
    local rownames
    forvalues j = 1/18 {
        local rownames `rownames' `=1989+`j''
    }
    matrix rownames X = `rownames'
    matrix list X
    This will work, at least this far. I have never before, however, seen a Stata matrix whose row or column names begin with digits. A quick review in the user manuals has not turned up anything to say that it is illegal, but it is also clear from the text and all examples that Stata matrix row and column names are often intended to match variable names--and, of course, a variable name cannot begin with a digit. I don't know what you plan to do with this matrix once you do this. It may well be that some subsequent matrix manipulations will fail, perhaps in strange ways, with these names. If it doesn't otherwise defeat your purpose, I would play it safe and rename the rows to yr1990, yr1991, etc. instead using the obvious modification of my code.

    Comment


    • #3
      Absolutely. Thanks!

      Dapel

      Comment


      • #4
        re: Clyde's post (#2 above), I have used matrices with rownames starting with digits - no problem

        Comment


        • #5
          Good to know this too. Thanks Rich

          Comment


          • #6
            No need for a loop here.

            Code:
             
            . mat z = J(18,4,42)
            
            . numlist "1990/2007"
            
            . mat rownames z = `r(numlist)'
            
            . mat li z
            
            z[18,4]
                  c1  c2  c3  c4
            1990  42  42  42  42
            1991  42  42  42  42
            1992  42  42  42  42
            1993  42  42  42  42
            1994  42  42  42  42
            1995  42  42  42  42
            1996  42  42  42  42
            1997  42  42  42  42
            1998  42  42  42  42
            1999  42  42  42  42
            2000  42  42  42  42
            2001  42  42  42  42
            2002  42  42  42  42
            2003  42  42  42  42
            2004  42  42  42  42
            2005  42  42  42  42
            2006  42  42  42  42
            2007  42  42  42  42

            Comment


            • #7
              Quite a straight one Dr Cox. Thanks.

              Comment


              • #8
                Is there a way to get rownames that would be something like 1990q3 to 1995q4?

                Comment


                • #9
                  I found a not quite as nice solution to my question:
                  Code:
                  gen tstr = string(t, "%tq")
                  levelsof tstr if inrange(t, t[10], t[15]), local(myrownames)     // choose whatever your desired time periods are
                  matrix rownames X = `myrownames'

                  Comment


                  • #10
                    It's a bit cumbersome, but:
                    Code:
                    mat z = J(22, 4, 42)
                    local rn
                    forvalues i= `=tq(1990q3)'/`=tq(1995q4)' {
                        local rn `rn' `:display %tq `i''
                    }
                    mat rownames z = `rn'
                    matrix list z

                    Comment


                    • #11
                      Thank you Clyde!

                      Comment


                      • #12
                        You can get a set of names in a local by pushing the dates in and out of Mata:

                        Code:
                        . mata
                        : t = (yq(1990,3)..yq(1995,4))
                        
                        : t
                                 1     2     3     4     5     6     7     8     9    10    11    12
                            +-------------------------------------------------------------------------
                          1 |  122   123   124   125   126   127   128   129   130   131   132   133
                            +-------------------------------------------------------------------------
                                13    14    15    16    17    18    19    20    21    22
                             -------------------------------------------------------------+
                          1    134   135   136   137   138   139   140   141   142   143  |
                             -------------------------------------------------------------+
                        
                        : names = strofreal(t, "%tq")
                        
                        : names
                                    1        2        3        4        5        6        7        8
                            +-------------------------------------------------------------------------
                          1 |  1990q3   1990q4   1991q1   1991q2   1991q3   1991q4   1992q1   1992q2
                            +-------------------------------------------------------------------------
                                    9       10       11       12       13       14       15       16
                             -------------------------------------------------------------------------
                          1    1992q3   1992q4   1993q1   1993q2   1993q3   1993q4   1994q1   1994q2
                             -------------------------------------------------------------------------
                                   17       18       19       20       21       22
                             -------------------------------------------------------+
                          1    1994q3   1994q4   1995q1   1995q2   1995q3   1995q4  |
                             -------------------------------------------------------+
                        
                        : end
                        So,

                        Code:
                         
                        . mata : st_local("names", invtokens(strofreal((yq(1990, 3)..yq(1995,4)), "%tq")))
                        
                        . di "`names'"
                        1990q3 1990q4 1991q1 1991q2 1991q3 1991q4 1992q1 1992q2 1992q3 1992q4 1993q1 1993q2 1993q3 1993q4 1994q1 1994q2 1994q3 1994q4 1995q1 1995q2 1995q3 1995q4
                        
                        .
                        Hence in your case you would just use the macro for your matrix row names.

                        Comment


                        • #13
                          Thank you Nick, appreciate it

                          Comment

                          Working...
                          X