[R] Easiest way to get the mean of a row of a data.frame?

Philipp Pagel p.pagel at gsf.de
Wed Dec 17 09:43:44 CET 2003


On Wed, Dec 17, 2003 at 08:27:41AM +0100, 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.)

rowMeans(USArrest)

does what you want.

In case you want to do soemthing else but compute the mean you will find
this handy:

apply(USArrests, 1, mean)

here you can substitute "mean" by whatever function you'd like to apply
to each row.

cu
	Philipp

-- 
Dr. Philipp Pagel                                Tel.  +49-89-3187-3675
Institute for Bioinformatics / MIPS              Fax.  +49-89-3187-3585
GSF - National Research Center for Environment and Health
Ingolstaedter Landstrasse 1
85764 Neuherberg, Germany




More information about the R-help mailing list