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

st: RE: Re: How to change matrix row names


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: How to change matrix row names
Date   Mon, 1 Nov 2004 23:08:00 -0000

No, I didn't say that, but I don't know of 
any really easy way. However, it would 
seem programmable, e.g. 

. matrix foo = J(5,5,1) 

. matrix rownames foo = "US citizens!" "Vote Kerry?" "Vote Bush?" 
"Your decision!" "But please vote"

. mat li foo

symmetric foo[5,5]
                 c1  c2  c3  c4  c5
   US citizens!   1
    Vote Kerry?   1   1
     Vote Bush?   1   1   1
 Your decision!   1   1   1   1
But please vote   1   1   1   1   1

. rowlabelschange foo, from(" ") to(_) 

. mat li foo

symmetric foo[5,5]
                 c1  c2  c3  c4  c5
   US_citizens!   1
    Vote_Kerry?   1   1
     Vote_Bush?   1   1   1
 Your_decision!   1   1   1   1
But_please_vote   1   1   1   1   1

program rowlabelschange 
	version 8 
	gettoken matname 0: 0, parse(" ,") 
	confirm matrix `matname' 
	syntax , from(str) to(str) 

	tempname row newmat 
	forval i = 1/`=rowsof(`matname')' { 
		mat `row' = `matname'[`i',1...] 
		local rowname : rownames `row' 
		local rowname : subinstr local rowname `"`from'"' `"`to'"', all
		mat rownames `row' = `rowname' 
		mat `newmat' = nullmat(`newmat')\(`row') 
	} 

	matrix colnames `newmat' = `: colnames `matname'' 
	matrix `matname' = `newmat' 
end 


Nick 
[email protected] 

Friedrich Huebler
 
> Nick, I take your message to mean that it is not possible to replace
> characters in matrix row names. In that case I will continue to use
> the solution suggested by Stata tech support, which changes value
> labels before any matrices are created.
> 
> My final goal is a tab-delimited text file that will be imported into
> a word processor to create a table like the one below. This table
> compares income across different groups in the same sample from a
> national household survey.
> 
> ===================
> Group        Median
>              income
> ===================
> Male           $XXX
> Female         $XXX
> -------------------
> Primary ed.    $XXX
> Secondary ed.  $XXX
> Higher ed.     $XXX
> -------------------
> Urban          $XXX
> Rural          $XXX
> -------------------
> New Jersey     $XXX
> New Mexico     $XXX
> New York       $XXX
> ===================
> Total          $XXX
> ===================
> 
> I save the output from several -tabstat, by()- commands in matrices,
> combine them in one big matrix, save this big matrix as variables
> with -svmat2-, and export the variables to a text file with
> -listtex-. Creating the table this way is faster than using
> -collapse- repeatedly because it does not require saving anything to
> my slow hard drive until the last step.
> 
> Friedrich Huebler
> 
> --- Nick Cox <[email protected]> wrote:
> > It's true that you are hitting a limitation of 
> > -matrix rownames- (which -svmat2- when written 
> > in 1999, and now, could do nothing about)
> > 
> > -----Original Message-----
> > I am looking for a way to save a matrix that was created with
> > -tabstat, by()-. An important assumption is that the value labels
> of
> > the -by- variable are not known in advance. I want to save the row
> > names together with the matrix values but don't know what to do
> when
> > the row names contain certain special characters like spaces or
> > apostrophes.

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