[R] computing the mean of a few variables

Gavin Simpson gavin.simpson at ucl.ac.uk
Thu Nov 1 09:52:36 CET 2007


On Thu, 2007-11-01 at 10:34 +0200, sigalit mangut-leiba wrote:
> hello,
> I have a simple question:
> I want to compute the average of 6 variables,
> my data looks like this:
> 
>     t0    t1   t2    t3   t4   t5
> 1  32   34  36   40   38  40
> 2  35   37  38   36   33  39
> .
> .
> .
> I want the mean of t0-t5 for each row (observation).
> thank you,
> sigalit.

Dear Sigalit,

See ?rowMeans

E.g.:

> dat <- scan()
1: 32   34  36   40   38  40
7: 35   37  38   36   33  39
13: 
Read 12 items
> dat <- data.frame(matrix(dat, nrow = 2, byrow = TRUE))
> names(dat) <- paste("t", 0:5, sep = "")
> dat
  t0 t1 t2 t3 t4 t5
1 32 34 36 40 38 40
2 35 37 38 36 33 39
> rowMeans(dat)
[1] 36.66667 36.33333
> 

HTH

G

> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list