[R] generic name of variables

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Jan 7 12:59:53 CET 2004


On Wed, 7 Jan 2004, SAULEAU Erik-André wrote:

> Dear R-list,
> 
> I wish a very happy new year and send you a little question: I have
> different variables which names are m1, m2, m4, .., m10, ... and want to
> obtain for example mean of each of them without typing each mean(m1),
> mean(m2), .... What is the solution for decomposing names in mXX?


Without knowing exactly the pattern, let me guess

nms <- ls(pattern="^m[0-9]+")
sapply(nms, function(m) mean(get(m)))

would do what you want.

As Thomas Lumley often says, it is probably better to use a list rather 
than m1, m2, m4, ....

-- 
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