Statalist


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

st: extract rowname corresponding to row index of matrix


From   Jacob Wegelin <[email protected]>
To   [email protected]
Subject   st: extract rowname corresponding to row index of matrix
Date   Wed, 1 Apr 2009 18:57:55 -0400

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 */

Thanks for any insights

Jacob A. Wegelin
Assistant Professor
Department of Biostatistics
Virginia Commonwealth University
730 East Broad Street Room 3006
P. O. Box 980032
Richmond VA 23298-0032
U.S.A.
E-mail: [email protected]
URL: http://www.people.vcu.edu/~jwegelin
*
*   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