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

From |
Ben Jann <ben.jann@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: extract rowname corresponding to row index of matrix |

Date |
Thu, 2 Apr 2009 09:32:56 +0200 |

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' <---> ben On Thu, Apr 2, 2009 at 12:57 AM, Jacob Wegelin <jacob.wegelin@gmail.com> 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 */ > > 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: jwegelin@vcu.edu > 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/ > * * 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/

**Follow-Ups**:**RE: st: extract rowname corresponding to row index of matrix***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

**References**:**st: extract rowname corresponding to row index of matrix***From:*Jacob Wegelin <jacob.wegelin@gmail.com>

- Prev by Date:
**Re: FW: st: Variance decomposition** - Next by Date:
**Re: st: RE: S-Curve/logistic curve regression** - Previous by thread:
**st: extract rowname corresponding to row index of matrix** - Next by thread:
**RE: st: extract rowname corresponding to row index of matrix** - Index(es):

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