Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | st: RE: format (decimals) in results stored |
Date | Wed, 2 Mar 2011 18:47:12 +0000 |
Here is a dopey example. . sysuse auto (1978 Automobile Data) . su mpg Variable | Obs Mean Std. Dev. Min Max -------------+-------------------------------------------------------- mpg | 74 21.2973 5.785503 12 41 . histogram mpg , subtitle(mean `: di %2.1f r(mean)' mpg) (bin=8, start=12, width=3.625) Know that the text "mean 21.3 mpg" appears as subtitle. The incantation here is `: di %2.1f r(mean)' which is documented more generally at -help extended_fcn- as -display display_directive-. Let's replay that as a two-step. 1. Given that you have r-class results in memory, you could go . di %2.1f r(mean) to see the mean formatted as you wish. 2. And you can also do that on the fly. Wrapping that in `: ' means that Stata will pretend to do that in mid-air, but the command concerned will see the result. In practice, I often do this outside the main call. I do something like local text : di %2.1f r(mean) and invoke the macro within the graph command. In practice, decide how many decimal places you want, then add one. So use formats %2.1f, %3.2f, etc. Don't think: oh, those numbers will be about 1000, so I need %5.1f. That way, you will just get awkward spaces when the number is in fact smaller. Use something like %2.1f and Stata will just assign enough space automagically, unless the number is very greatly bigger. If your numbers are highly predictable, use something other than an f-format. Nick n.j.cox@durham.ac.uk Leandro Brufman I am trying to include results stored in r() in graphs. For instance, to add them as labels in graphs - with suboptions xlabel(0(1)10 `r(mean)' "Mean") - The only problem I have is that r(mean) includes a lot of decimals. I would like to change the format, but only to the `r(mean)' (ie, its not helpful to change the dispaly format of the whole x - i want integers from 0 to 10 and then the mean with two decimals) * * 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/