Stefan Fleck
2022-Jan-30 09:16 UTC
[R] Weird behaviour of order() when having multiple ties
I am experiencing a weird behavior of `order()` for numeric vectors. I tested on 3.6.2 and 4.1.2 for windows and R 4.0.2 on ubuntu. Can anyone confirm? order( c( 0.6, 0.5, 0.3, 0.2, 0.1, 0.1 ) ) ## Result [should be in order] [1] 5 6 4 3 2 1 The sort order is obviously wrong. This only occurs if i have multiple ties. The problem does _not_ occur for decreasing = TRUE. [[alternative HTML version deleted]]
Richard M. Heiberger
2022-Jan-30 20:00 UTC
[R] [External] Weird behaviour of order() when having multiple ties
when there are ties it doesn't matter which is first. in a situation where it does matter, you will need a tiebreaker column. ________________________________ From: R-help <r-help-bounces at r-project.org> on behalf of Stefan Fleck <stefan.b.fleck at gmail.com> Sent: Sunday, January 30, 2022 4:16:44 AM To: r-help at r-project.org <r-help at r-project.org> Subject: [External] [R] Weird behaviour of order() when having multiple ties I am experiencing a weird behavior of `order()` for numeric vectors. I tested on 3.6.2 and 4.1.2 for windows and R 4.0.2 on ubuntu. Can anyone confirm? order( c( 0.6, 0.5, 0.3, 0.2, 0.1, 0.1 ) ) ## Result [should be in order] [1] 5 6 4 3 2 1 The sort order is obviously wrong. This only occurs if i have multiple ties. The problem does _not_ occur for decreasing = TRUE. [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=04%7C01%7Crmh%40temple.edu%7Cbae20314c2314a5cc7cd08d9e429e33f%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637791692024451993%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=O6R%2FNM6IdPzP8RY3JIWfLgmkE%2B0KcVyYBxoRMo8v2dk%3D&reserved=0 PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Crmh%40temple.edu%7Cbae20314c2314a5cc7cd08d9e429e33f%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637791692024451993%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000&sdata=6hlfMjZLzopVzGnFVWlGnoEqvZBQwXPlxMuZ2sglEUk%3D&reserved=0 and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Stefan, I did not replicate your results as it works fine and applying the order result in a sorted properly result: c(0.6, 0.5, 0.3, 0.2, 0.1, 0.1)[order(c(0.6, 0.5, 0.3, 0.2, 0.1, 0.1))] [1] 0.1 0.1 0.2 0.3 0.5 0.6 I am using a more recent version of R, but for something this basic, it should not matter as even older versions should do this fine. R version 4.1.2 (2021-11-01) -- "Bird Hippie" Copyright (C) 2021 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 (64-bit) Consider restarting your R cleanly with no variables in place before reporting an error. -----Original Message----- From: Stefan Fleck <stefan.b.fleck at gmail.com> To: r-help at r-project.org Sent: Sun, Jan 30, 2022 4:16 am Subject: [R] Weird behaviour of order() when having multiple ties I am experiencing a weird behavior of `order()` for numeric vectors. I tested on 3.6.2 and 4.1.2 for windows and R 4.0.2 on ubuntu. Can anyone confirm? order( ? c( ? ? 0.6, ? ? 0.5, ? ? 0.3, ? ? 0.2, ? ? 0.1, ? ? 0.1 ? ) ) ## Result [should be in order] [1] 5 6 4 3 2 1 The sort order is obviously wrong. This only occurs if i have multiple ties. The problem does _not_ occur for decreasing = TRUE. ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.
Rui Barradas
2022-Jan-30 20:31 UTC
[R] Weird behaviour of order() when having multiple ties
Hello, I am not seeing an error, the order is right: x <- c( 0.6, 0.5, 0.3, 0.2, 0.1, 0.1 ) (i <- order(x)) #> [1] 5 6 4 3 2 1 x[i] #> [1] 0.1 0.1 0.2 0.3 0.5 0.6 Hope this helps, Rui Barradas ?s 09:16 de 30/01/2022, Stefan Fleck escreveu:> I am experiencing a weird behavior of `order()` for numeric vectors. I > tested on 3.6.2 and 4.1.2 for windows and R 4.0.2 on ubuntu. Can anyone > confirm? > > order( > c( > 0.6, > 0.5, > 0.3, > 0.2, > 0.1, > 0.1 > ) > ) > ## Result [should be in order] > [1] 5 6 4 3 2 1 > > The sort order is obviously wrong. This only occurs if i have multiple > ties. The problem does _not_ occur for decreasing = TRUE. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.
Jeff Newmiller
2022-Jan-30 21:14 UTC
[R] Weird behaviour of order() when having multiple ties
Why should 6,5 be more correct than 5,6? How is R supposed to reach that conclusion based on comparing values? On January 30, 2022 1:16:44 AM PST, Stefan Fleck <stefan.b.fleck at gmail.com> wrote:>I am experiencing a weird behavior of `order()` for numeric vectors. I >tested on 3.6.2 and 4.1.2 for windows and R 4.0.2 on ubuntu. Can anyone >confirm? > >order( > c( > 0.6, > 0.5, > 0.3, > 0.2, > 0.1, > 0.1 > ) >) >## Result [should be in order] >[1] 5 6 4 3 2 1 > >The sort order is obviously wrong. This only occurs if i have multiple >ties. The problem does _not_ occur for decreasing = TRUE. > > [[alternative HTML version deleted]] > >______________________________________________ >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >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.-- Sent from my phone. Please excuse my brevity.