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? With my best regards, EA. ===========================================Erik-André SAULEAU SEAIM Hôpital du Hasenrain BP 1070 87, Avenue de Altkirch 68051 MULHOUSE Cédex Tel: 03-89-64-79-95 Fax: 03-89-64-79-71 Mél: sauleauea@ch-mulhouse.fr Web: http://www.ch-mulhouse.fr =========================================== ************************************************************************************ Afin d'éviter toute propagation de virus informatique, et en complément des dispositifs en place, ce message (et ses pièces jointes s'il y en a) a été automatiquement analysé par un antivirus de messagerie. *********************************************************************************** [[alternative HTML version deleted]]
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