Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: rename individual columns/rows of a matrix


From   "Daniel Stegmueller" <[email protected]>
To   [email protected]
Subject   Re: st: RE: rename individual columns/rows of a matrix
Date   Sun, 19 Nov 2006 16:45:01 +0100

Nick,

thank you very much for your suggestions!

Best,
Daniel




2006/11/17, Nick Cox <[email protected]>:
I think whatever you do will end in something like

matrix rownames ... = ...

or

matrix colnames ... = ...

where the RHS includes the new names. This is adoable,
as I understand it. The question is whether the result
is any less awkward than doing it directly. Here is one stab,
not tested much.

-------------------------------------------- matrcrename.ado
// NJC 1.0.0 17 November 2006
// matrix row_or_column rename
program matrcrename
        version 8
        // syntax matrixname row_or_col which_row_or_col new_name

        // matrix name
        gettoken matrix 0 : 0
        confirm matrix `matrix'

        // row or column
        gettoken which 0 : 0
        local length = length("`which'")
        if lower("`which'") == substr("row",1,`length') {
                local which row
        }
        else if lower("`which'") == substr("column",1,`length') {
                local which col
        }
        else {
                di as err "second argument should specify row or column"
                exit 198
        }

        // which row or column
        gettoken where newname : 0
        if "`which'" == "row" {
                capture local found = inrange(`where', 1, rowsof(`matrix'))
                if _rc {
                        di as err "inappropriate row number?"
                        exit 498
                }
                if !`found' {
                        di as err "row out of range"
                        exit 498
                }
        }
        else {
                capture local found = inrange(`where', 1, colsof(`matrix'))
                if _rc {
                        di as err "inappropriate column number?"
                        exit 498
                }
                if !`found' {
                        di as err "column out of range"
                        exit 498
                }
        }

        // test newname
        tempname moo
        matrix `moo' = J(1,1,1)
        capture matrix rownames `moo' = `newname'
        if _rc {
                local what = cond("`which'" == "col", "column", "row")
                di as err "inappropriate `what' name?"
                exit 498
        }

        // in business!
        local names : `which'names `matrix'
        tokenize `names'
        local `where' `newname'
        local newnames "`*'"
        matrix `which'names `matrix' = `newnames'
end
--------------------------------------------

Examples:
-----------------------------------------------------------------------------------------------------------
. mat li z

symmetric z[3,3]
   X  Y  Z
a  1
b  1  1
c  1  1  1

. matrcrename z row 3 Stata

. mat li z

symmetric z[3,3]
       X  Y  Z
    a  1
    b  1  1
Stata  1  1  1

. matrcrename z col 4 SAS
column out of range
r(498);

. matrcrename z col 3 Some Alternative Software
inappropriate column name?
r(498);

. matrcrename frog col 3 toad
matrix frog not found
r(111);

----------------------------------------------------------------------------------------------------------

Nick
[email protected]

Daniel Stegmueller

> is there a quick way to change individual columns or rows of a matrix?
> The 'matrix rownames' command is not useful, since I want to use a
> loop to rename rows and columns step-by step.

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


--
Blore's Razor:
       Given a choice between two theories, take the one which is funnier.


------------------------------------------------------------
Daniel Stegm�ller
Elisabethstr. 3
68165 Mannheim

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index