[R] sort a list?

rkevinburton at charter.net rkevinburton at charter.net
Tue Oct 7 19:53:18 CEST 2008


THank you. I just didn't know the "rules". In other languages it is possible to pass in a 'compare' function so the sort is defined by the function. I guess I was a) wondering why it failed, and b) if there was a similar work around to sort generic lists. Also I was specifically addressing the list that I gave in the example not a generic list. I think I have a solution. 

Thank you.

Kevin
---- Erik Iverson <iverson at biostat.wisc.edu> wrote: 
> Since objects of class "list" in R can be made up of heterogeneous 
> objects, sorting them does not make much sense.  For example, does "A", 
> come before or after 1000, does a linear model summary come before or 
> after pi?
> 
> If your data are all numeric, store them as a numeric vector, where sort 
> works.  Vectors can be named in R, as in your example.
> 
> Also, 'order' does something different than 'sort'.
> 
> try the following:
> 
> x <- c(a = 1, b = 4, c = 2)
> sort(x)
> order(x)
> 
> Hope that helps,
> Erik
> 
> 
> 
> rkevinburton at charter.net wrote:
> > I am trying to sort a list and the data is obiously not in the right format. I am trying:
> > 
> > x <- list()
> > x[["A"]] <- 1
> > x[["B"]] <- 2
> > 
> > order(x)
> > 
> > But am getting:
> > 
> > Error in order(x) : unimplemented type 'list' in 'orderVector1'
> > 
> > How should I change the list so that it can be sorted? What kinds of objects (classes of objects) can be sorted?
> > 
> > Thank you.
> > 
> > Kevin
> > 
> > ______________________________________________
> > 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.



More information about the R-help mailing list