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
Try this: x[order(unlist(x), decreasing = TRUE)] On Tue, Oct 7, 2008 at 1:28 PM, <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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
Exactly what were your expections from sorting the list? What did you expect the answer to look like? You can 'unlist' the list and then sort the elements:> x[["A"]] <- 1:10 > x[["B"]] <- 4:12 > sort(unlist(x))A1 A2 A3 A4 B1 A5 B2 A6 B3 A7 B4 A8 B5 A9 B6 A10 B7 B8 B9 1 2 3 4 4 5 5 6 6 7 7 8 8 9 9 10 10 11 12>Is this what you want? On Tue, Oct 7, 2008 at 12:28 PM, <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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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.
There was discussion about language desing here recently -- other languages are aparently badly designed. They: 1. Allow you to sort lists, which is bad idea. 2. Probably use name like 'sort.list' for that purpose, which is even worse idea. 3. Do not respond 'error: have you called sort on a list' when you call sort.list on a list, which is completely crazy. Stop using other languages. Learn the rules, there are plenty. \misiek> 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@biostat.wisc.edu> <iverson@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@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@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 <http://www.r-project.org/posting-guide.html> > > and provide commented, minimal, self-contained, reproducible code.[[alternative HTML version deleted]]