Hello r-help I am learning R and use R-studio. I create vector x <- c(19,17,23,11) and use function order(x). The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. Follow picture that i attach. Thank you for you answer. -------------- next part -------------- A non-text attachment was scrubbed... Name: Screenshot (5).png Type: image/png Size: 68434 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170718/2df88884/attachment.png> -------------- next part -------------- A non-text attachment was scrubbed... Name: Screenshot (6).png Type: image/png Size: 145778 bytes Desc: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20170718/2df88884/attachment-0001.png>
Hi Jesadaporn, Try: order(x,decreasing=TRUE) Jim On Tue, Jul 18, 2017 at 12:58 PM, Jesadaporn Pupantragul <jp.beckmcs at gmail.com> wrote:> Hello r-help > I am learning R and use R-studio. > I create vector x <- c(19,17,23,11) and use function order(x). > The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. > Follow picture that i attach. > Thank you for you answer. > > ______________________________________________ > 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.
You need to study ?order and perhaps also subscripting. If that isn't sufficient, I suggest you consult one of the many R web tutorials that cover this. Perhaps this will help: x[order(x)] gives x in sorted order, which is what you woud get with sort(x). Indeed, the code implementing sort.default(x) is essentially x[order(x)] ! Cheers, Bert Bert Gunter "The trouble with having an open mind is that people keep coming along and sticking things into it." -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip ) On Mon, Jul 17, 2017 at 7:58 PM, Jesadaporn Pupantragul <jp.beckmcs at gmail.com> wrote:> Hello r-help > I am learning R and use R-studio. > I create vector x <- c(19,17,23,11) and use function order(x). > The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. > Follow picture that i attach. > Thank you for you answer. > > ______________________________________________ > 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.
On 18/07/17 14:58, Jesadaporn Pupantragul wrote:> Hello r-help > I am learning R and use R-studio. > I create vector x <- c(19,17,23,11) and use function order(x). > The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. > Follow picture that i attach. > Thank you for you answer.Perhaps this will give you some insight: o <- order(x) x[o] cheers, Rolf Turner -- Technical Editor ANZJS Department of Statistics University of Auckland Phone: +64-9-373-7599 ext. 88276
I think you want rank, not order.> x <- c(19,17,23,11) > order(x)[1] 4 2 1 3> rank(x)[1] 3 2 4 1 See help(order) and help(rank) for the difference. Peter On Mon, Jul 17, 2017 at 7:58 PM, Jesadaporn Pupantragul <jp.beckmcs at gmail.com> wrote:> Hello r-help > I am learning R and use R-studio. > I create vector x <- c(19,17,23,11) and use function order(x). > The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. > Follow picture that i attach. > Thank you for you answer. > > ______________________________________________ > 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.
On Tue, 18 Jul 2017 09:58:19 +0700 Jesadaporn Pupantragul <jp.beckmcs at gmail.com> wrote:> Hello r-help > I am learning R and use R-studio. > I create vector x <- c(19,17,23,11) and use function order(x). > The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. > Follow picture that i attach. > Thank you for you answer.You might want to ask why you would ever see "[1] 3 2 4 1" as a result of order(). A descending order would yield [1] 3 1 2 4. JDougherty
The definition of 'order' is that x[order(x)] is in increasing order. E.g., > x <- c(19,17,23,11) > order(x) [1] 4 2 1 3 > x[order(x)] [1] 11 17 19 23 You may be looking for what 'rank' does: > rank(x) [1] 3 2 4 1 (If x has no ties, then rank(x) is order(order(x)).) Bill Dunlap TIBCO Software wdunlap tibco.com On Mon, Jul 17, 2017 at 7:58 PM, Jesadaporn Pupantragul < jp.beckmcs at gmail.com> wrote:> Hello r-help > I am learning R and use R-studio. > I create vector x <- c(19,17,23,11) and use function order(x). > The result show [1] 4 2 1 3. Why it doesn't show [1] 3 2 4 1. > Follow picture that i attach. > Thank you for you answer. > > ______________________________________________ > 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. >[[alternative HTML version deleted]]