Hello, I wonder if I ran into a bug. If I do summary(df1$X1) -> df1.y df1.y a b c d e [1,] 50.74627 8.955224 17.91045 19.40299 2.985075 sort(df1.y) a b c d e [1,] 2.985075 8.955224 17.91045 19.40299 50.74627 my numbers are sorted but do not anymore correspond to the rownames. For me it is counterintuitive that solely the numbers are sorted and not the names. Is there a way to sort names + numbers or is this behaviour of sort() unintended? Martin R 2.0.1-1 debian reposit.
Martin Wegmann wrote:> Hello, > > I wonder if I ran into a bug. If I do > > summary(df1$X1) -> df1.y > > df1.y > a b c d e > [1,] 50.74627 8.955224 17.91045 19.40299 2.985075 > > sort(df1.y) > a b c d e > [1,] 2.985075 8.955224 17.91045 19.40299 50.74627Note: You are sorting a matrix rather than a vector!> my numbers are sorted but do not anymore correspond to the rownames.You mean colnames, not rownames!> For me it is counterintuitive that solely the numbers are sorted and not the > names. Is there a way to sort names + numbers or is this behaviour of sort() > unintended?Don't use sort() on matrices, use order() instead, as in: x[,order(x)] Uwe Ligges> Martin > > R 2.0.1-1 debian reposit. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Hi I am not sure how you did get such result of sort and summary Summary usually gives> summary(titr$kys)Min. 1st Qu. Median Mean 3rd Qu. Max. 0.000 1.050 1.250 1.143 1.369 2.000 and sorting it> y<-summary(titr$kys) > y[1]<-5 > y[3]<-10 > yMin. 1st Qu. Median Mean 3rd Qu. Max. 5.000 1.050 10.000 1.143 1.369 2.000> sort(y)1st Qu. Mean 3rd Qu. Max. Min. Median 1.050 1.143 1.369 2.000 5.000 10.000 as well as> x<-rnorm(5)*5 > names(x)<-letters[1:5] > xa b c d e -3.8849080 1.9393499 1.8456569 -2.5334288 0.6304933> sort(x)a d e c b -3.8849080 -2.5334288 0.6304933 1.8456569 1.9393499 On 14 Dec 2004 at 12:23, Martin Wegmann wrote: Didn't you redefine something? Cheers Petr> Hello, > > I wonder if I ran into a bug. If I do > > summary(df1$X1) -> df1.y > > df1.y > a b c d e > [1,] 50.74627 8.955224 17.91045 19.40299 2.985075 > > sort(df1.y) > a b c d e > [1,] 2.985075 8.955224 17.91045 19.40299 50.74627 > > my numbers are sorted but do not anymore correspond to the rownames. > For me it is counterintuitive that solely the numbers are sorted and > not the names. Is there a way to sort names + numbers or is this > behaviour of sort() unintended? > > Martin > > R 2.0.1-1 debian reposit. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.htmlPetr Pikal petr.pikal at precheza.cz
I asked the same question a few weeks ago. See http://tolstoy.newcastle.edu.au/R/help/04/11/6775.html -----Original Message----- From: Martin Wegmann Sent: Tuesday, December 14, 2004 6:23 AM To: r-help at stat.math.ethz.ch Subject: [R] sort() leaves row names unaffected Hello, I wonder if I ran into a bug. If I do summary(df1$X1) -> df1.y df1.y a b c d e [1,] 50.74627 8.955224 17.91045 19.40299 2.985075 sort(df1.y) a b c d e [1,] 2.985075 8.955224 17.91045 19.40299 50.74627 my numbers are sorted but do not anymore correspond to the rownames. For me it is counterintuitive that solely the numbers are sorted and not the names. Is there a way to sort names + numbers or is this behaviour of sort() unintended? Martin R 2.0.1-1 debian reposit. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __________________________________ Dress up your holiday email, Hollywood style. Learn more.
The problem, I think, boils down to the following:> x <- matrix(c(3, 1, 2), ncol=3, dimnames=list(NULL, letters[1:3])) > xa b c [1,] 3 1 2> sort(x)a b c [1,] 1 2 3> y <- rbind(x, c(1,1,2)) > ya b c [1,] 3 1 2 [2,] 1 1 2> sort(y)a b c [1,] 1 1 2 [2,] 1 2 3 I.e., I'm guessing your `df1.y' is a matrix with one row and column names `a' through `e'. When you sort() a matrix, sort() simply treat it as a vector (as a matrix in R is just a vector with a dim attribute) and sort that (by stacking columns). The output is sorted, but with the original dimension and dimnames as the input. Perhaps sort() should be made generic with methods for matrix/array/data.frame, but one needs to think about what the logical thing to do if no other argument is given... In any case, it shouldn't give you the result you're expecting (at least not by default): you are expecting the result to be sorted columns, which hardly makes sense if there are more than one row. Andy> From: Martin Wegmann > > Hello, > > I wonder if I ran into a bug. If I do > > summary(df1$X1) -> df1.y > > df1.y > a b c d e > [1,] 50.74627 8.955224 17.91045 19.40299 2.985075 > > sort(df1.y) > a b c d e > [1,] 2.985075 8.955224 17.91045 19.40299 50.74627 > > my numbers are sorted but do not anymore correspond to the rownames. > For me it is counterintuitive that solely the numbers are > sorted and not the > names. Is there a way to sort names + numbers or is this > behaviour of sort() > unintended? > > Martin > > R 2.0.1-1 debian reposit. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >
I don't see how this is the `same' question at all. The object Martin has doesn't look like a data frame (can you tell why?), so I believe is unrelated to what you asked before. Andy ps: Why is this copied to debian_list at web.de?> From: bogdan romocea > > I asked the same question a few weeks ago. See > http://tolstoy.newcastle.edu.au/R/help/04/11/6775.html > > > -----Original Message----- > From: Martin Wegmann > Sent: Tuesday, December 14, 2004 6:23 AM > To: r-help at stat.math.ethz.ch > Subject: [R] sort() leaves row names unaffected > > > Hello, > > I wonder if I ran into a bug. If I do > > summary(df1$X1) -> df1.y > > df1.y > a b c d e > [1,] 50.74627 8.955224 17.91045 19.40299 2.985075 > > sort(df1.y) > a b c d e > [1,] 2.985075 8.955224 17.91045 19.40299 50.74627 > > my numbers are sorted but do not anymore correspond to the rownames. > For me it is counterintuitive that solely the numbers are sorted and > not the > names. Is there a way to sort names + numbers or is this behaviour of > sort() > unintended? > > Martin > > R 2.0.1-1 debian reposit. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > > > > __________________________________ > > Dress up your holiday email, Hollywood style. Learn more. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >