Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: matrix row/col names


From   "Jann, Ben" <ben.jann@soz.gess.ethz.ch>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: matrix row/col names
Date   Thu, 4 Sep 2003 15:49:25 +0200

John asks:
> > I've got two related questions regarding column and row names.
> > (1) How can I replace the 'jth' column name only?
> > (2) How can I save as a macro the 1st j column names from a matrix?

Kit answers:
> Answering in reverse order (see [p] macro extended functions):
> 
> . qui regress price mpg headroom trunk
> 
> . mat v=e(V)
> 
> . local names: colnames v
> 
> . di "`names'"
> mpg headroom trunk _cons
> 
> . local names: subinstr local names "trunk" "elephant"
> 
> . di "`names'"
> mpg headroom elephant _cons
> 
> . mat coln v= `names'

Using -subinstr- was also my first thought when I read John's questions.
Be aware, however, that this will only work correctly, if the rownames
are unique (option -word- makes it a little better, but not much).

Concering the problem with spaces Nick wrote:
> To develop a key point, you can assign
> a row or column name including one or
> more spaces, but you cannot retrieve
> it (easily).

The following code will probably do (replacing row names):

 mat A = J(2,2,1) 
 mat rownames A = "two words" "three more words"
 mat li A 
 tempname B
 forv i=1/`=rowsof(A)' {
  if `i'==2 local rnames `"`rnames'"what ever" "'
  else {
   mat `B'=A[`i'..`i',1..1]
   local rnames `"`rnames'"`:rownames(`B')'" "'
   }
  }
 mat rownames A=`rnames'
 mat list A

ben

*
*   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–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index