A problem with order() function in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

A problem with order() function in R

Jesadaporn Pupantragul
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.

______________________________________________
[hidden email] 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.

Screenshot (5).png (91K) Download Attachment
Screenshot (6).png (194K) Download Attachment
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A problem with order() function in R

Jim Lemon-4
Hi Jesadaporn,
Try:

order(x,decreasing=TRUE)

Jim

On Tue, Jul 18, 2017 at 12:58 PM, Jesadaporn Pupantragul
<[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A problem with order() function in R

Bert Gunter-2
In reply to this post by Jesadaporn Pupantragul
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
<[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [FORGED] A problem with order() function in R

Rolf Turner
In reply to this post by Jesadaporn Pupantragul
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

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A problem with order() function in R

plangfelder
In reply to this post by Jesadaporn Pupantragul
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
<[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A problem with order() function in R

JohnDee
In reply to this post by Jesadaporn Pupantragul
On Tue, 18 Jul 2017 09:58:19 +0700
Jesadaporn Pupantragul <[hidden email]> 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

______________________________________________
[hidden email] 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.
Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: A problem with order() function in R

R help mailing list-2
In reply to this post by Jesadaporn Pupantragul
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 <
[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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]]

______________________________________________
[hidden email] 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.
Loading...