[R] how to sort the contents of a list-object?
Prof Brian D Ripley
ripley at stats.ox.ac.uk
Wed May 12 10:39:42 CEST 2004
On Wed, 12 May 2004, Martin Kerick wrote:
> I have a list-object RGList named RG containing two vectors (RG$R and RG$G)
> and a data.frame(RG$genes) with 8 variables. Each of the variables of the
> data.frame have the same length as the two vectors. I think the data in the
> RGList is structured in such a way, that the first entry in the vector RG$R
> belongs to the first entry in the data.frame variable e.g. RG$genes$Row.
> Now my question:
> Is it possible to sort the RGList object by the values of one variable of my
> data.frame e.g. RG$genes$Row without loosing the ties between the different
> parts of the list ?
Yes. Did you also want to know how? If so, see later.
But why? It looks like R and G should be additional columns of the data
frame and if you had a better data organization you would *know* what
matched what and operations like this would be obvious.
ord <- sort.list(RG$genes$Row)
RG <- lapply(RG, function(x) if(is.data.frame) x[ord,] else x[ord])
--
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 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list