Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: extract rowname corresponding to row index of matrix


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: extract rowname corresponding to row index of matrix
Date   Thu, 2 Apr 2009 12:32:27 +0100

I think Ben slightly understates how easy it is -- once you know the syntax, naturally. 

. mat A = (1, 1 \ 2, 2)

. mat rownames A = one two

Here are two further variations on how to do it. One nests function calls: 

. di "`: word 2 of `: rownames A''"
two

The other uses -tokenize- first: 

. tokenize "`: rownames A'"

. di "`2'"
two

Nick 
[email protected] 

Ben Jann

The trick is as follows:

<--->
matrix input BETA=( 193\  30 \ 133 \ 51\  9.8 \ 0.1 )
matrix rownames BETA= dog cat rat mouse flea bubonic
matrix colnames BETA=IQ
mat lis BETA

local r = rownumb(matrix(BETA),"bubonic")
local c = colnumb(matrix(BETA),"IQ")

matrix RowNamesNoLongerLost=BETA[`r',1...]
matrix RowNamesNoLongerLost=RowNamesNoLongerLost[1...,`c']
mat lis RowNamesNoLongerLost
<--->

However, if you just want to extract the names (and if you know that
the names do not contain blanks!) you can also simply type:

<--->
local colnms: coln BETA
local rownms: rown BETA
local colname: word 1 of `colnms'
local rowname: word 6 of `rownms'
<--->

On Thu, Apr 2, 2009 at 12:57 AM, Jacob Wegelin <[email protected]> wrote:
> Suppose we have a matrix BETA with -rownames- and -colnames-.
>
> Given the names of a row and of a column in BETA, it is possible to
> extract the indices of that row and of that column, as shown below.
>
> But is there an equally simple approach to the reverse problem?  I
> mean: Given a row index, is there an elegant way to extract the name
> of that row?
>
> The solution below seems clumsy. As seen with -matrix LostRowNames-,
> extracting a one-row matrix from a one-column matrix causes the
> -rownames- to be lost. Thus my program uses a column join, which seems
> like a kludge.
>
> /* BEGIN STATA CODE */
>
> clear all
> matrix input BETA=( 193\  30 \ 133 \ 51\  9.8 \ 0.1 )
> matrix rownames BETA= dog cat rat mouse flea bubonic
> matrix colnames BETA=IQ
> mat lis BETA
>
> di "index of row called bubonic is " rownumb(matrix(BETA),"bubonic")
> di "index of column called IQ is " colnumb(matrix(BETA),"IQ")
>
> matrix LostRowNames=BETA[1,1]
> mat lis LostRowNames
>
> capture program drop getRowName
> program define getRowName, rclass
>   local thismatrix="`1'"
>   local rowindex=`2'
>   matrix TWO= `thismatrix', `thismatrix'
>   matrix TEMPORARY=TWO[`rowindex',1..2]
>   local rowname: rownames TEMPORARY
>   return local matrixname="`thismatrix'"
>   return scalar rowindex=`rowindex'
>   return local rowname = "`rowname'"
> end
>
> getRowName BETA 1
> return list
> getRowName BETA 5
> return list
>
> /*  END STATA CODE */
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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