[R] Easiest way to get the mean of a row of a data.frame?
    Prof Brian Ripley 
    ripley at stats.ox.ac.uk
       
    Wed Dec 17 08:42:32 CET 2003
    
    
  
rowMeans(mydf)  will do this for a data frame mydf.
Be careful though to use it only with all-numeric data frames (there is an
implicit as.matrix going on), and often it makes more sense to store such
data in a matrix.
apply(mydf, 1, mean) would also work, but is slower.
On Wed, 17 Dec 2003, Sven C. Koehler wrote:
> I am kind of new to R.
> I would like to calculate the mean of the numbers of this expression:
> 
>     data(USArrests)
>     USArrests[row.names(M) == "Alabama",]
> 
> class() tells me it's a ``data.frame,'' what I actually desire is to get
> all numbers of a row as a vector or a list to let mean() calculate the mean
> of the whole row.  (I know this doesn't make sense with USArrests, I just
> used it here instead of my very own data.frame.)
-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595
    
    
More information about the R-help
mailing list