Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the order() function

Tags:

sorting

r

r-faq

I'm trying to understand how the order() function works. I was under the impression that it returned a permutation of indices, which when sorted, would sort the original vector.

For instance,

> a <- c(45,50,10,96) > order(a) [1] 3 1 2 4 

I would have expected this to return c(2, 3, 1, 4), since the list sorted would be 10 45 50 96.

Can someone help me understand the return value of this function?

like image 591
jeffshantz Avatar asked Feb 23 '10 01:02

jeffshantz


People also ask

How do you order values in R?

To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.

What is the difference between sort and order in R?

Here's the difference between these functions: sort() will sort a vector in ascending order. order() will return the index of each element in a vector in sorted order. rank() will assign a rank to each element in a vector (smallest = 1)

How do you arrange a vector in ascending order in R?

To sort a Vector in R, use the sort() function. By default, R will sort the vector in ascending order. However, you can add the decreasing argument to the function, explicitly specifying the sort order.

How do you reverse order in R?

The rev() method in R is used to return the reversed order of the R object, be it dataframe or a vector. It computes the reverse columns by default. The resultant dataframe returns the last column first followed by the previous columns. The ordering of the rows remains unmodified.


1 Answers

This seems to explain it.

The definition of order is that a[order(a)] is in increasing order. This works with your example, where the correct order is the fourth, second, first, then third element.

You may have been looking for rank, which returns the rank of the elements
R> a <- c(4.1, 3.2, 6.1, 3.1)
R> order(a)
[1] 4 2 1 3
R> rank(a)
[1] 3 2 4 1
so rank tells you what order the numbers are in, order tells you how to get them in ascending order.

plot(a, rank(a)/length(a)) will give a graph of the CDF. To see why order is useful, though, try plot(a, rank(a)/length(a),type="S") which gives a mess, because the data are not in increasing order

If you did
oo<-order(a)
plot(a[oo],rank(a[oo])/length(a),type="S")
or simply
oo<-order(a)
plot(a[oo],(1:length(a))/length(a)),type="S")
you get a line graph of the CDF.

I'll bet you're thinking of rank.

like image 82
duffymo Avatar answered Sep 27 '22 02:09

duffymo