Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.44.0401071151310.1110-100000@gannet.stats>
Date: 2004-01-07T11:59:53Z
From: Brian Ripley
Subject: generic name of variables
In-Reply-To: <A91EF0B9121F834EA6484582DFE1CF4436F09E@messagerie.chm.com>

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