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

From |
"Martin Weiss" <martin.weiss1@gmx.de> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
Re: st: row mean (mean across columns) |

Date |
Wed, 8 Oct 2008 21:14:29 +0200 |

sysuse auto, clear qui ds,has(type numeric) local varlist `r(varlist)' egen miss = rowmiss(_all) egen mean = rowmean(`varlist') if miss==0 HTH Martin _______________________

To: <statalist@hsphsun2.harvard.edu> Sent: Wednesday, October 08, 2008 9:01 PM Subject: Re: st: row mean (mean across columns)

Jacob, 2008/10/8 Jacob Wegelin <jwegelin@vcu.edu>:Given any dataset of all numeric variables, I want to generate a new variable called myMean, which is the arithmetic mean (the average) acrossall the variables. The program below solves this problem. But surelythereis a one-line command that will perform this task in Stata? The post http://www.stata.com/statalist/archive/2008-09/msg00597.html appears to contain a bug, in the sense that the row total computed is not corrected as in my code below. This should be done in a general manner: (1) As in the current dataset, the variables will not necessarily be in a form like a1 to a100. (2) The number of variables is arbitrary, so I cannot hard-code the denominator as when myMeanByHand is computed below. (3) If any value in a row is missing (.), the mean computed must also bemissing, since then the mean across all variables is not defined. (Thusegenrowtotal is not the answer.)I might be misunderstanding you, but wouldn't qui ds local varlist `r(varlist)' egen miss = rowmiss(_all) egen mean = rowmean(`varlist') if miss==0 be a solution? It will calculate the average you want, in a flexible manner, but only if there are no missings anywhere in the observation./* A related question: The following gives an incorrect answer. What intheworld is it doing? */ egen junk=rowmean(*) listInteresting one. I did a test, with the auto data: sysuse auto drop make egen junk = rowmean(_all) sum junk drop junk egen junk2 = rowmean(*) sum junk2 and also received different results. Setting -trace- on revealed that in the second case, Stata seems to be including a temporary variable (sort order?) into the calculation. It does look like a bug to me. HTH, Eva * * 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/

**References**:**st: row mean (mean across columns)***From:*Jacob Wegelin <jwegelin@vcu.edu>

**Re: st: row mean (mean across columns)***From:*"Eva Poen" <eva.poen@gmail.com>

- Prev by Date:
**Re: st: row mean (mean across columns)** - Next by Date:
**st: RE: RE: Tabulate** - Previous by thread:
**Re: st: row mean (mean across columns)** - Next by thread:
**st: RE: row mean (mean across columns)** - Index(es):

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