In R, there are three main functions related to sorting: sort (), rank (), order ().
The sort (x) is the vector x that is sorted, and the return value is sorted by the numeric vector. Rank () is a function of rank, and its return value is the "rank" of the corresponding element in the vector. The return value of order () is the position in the vector that corresponds to the "rank" element.
Here is an example of a small section of R code:
> x<-c (97,93,85,74,32,100,99,67)
> Sort (x)
[1] 32 67 74 85 93 97 99 100
> Order (x)
[1] 5 8 4 3 2 1 7 6
> Rank (x)
[1] 6 5 4 3 1 8 7 2
Suppose X is the amount of time it takes for a group of students to complete a test (the shorter the time, the higher the ranking), the return value of rank () is the rank of the group of students, and the return value of order () is the position in the vector of the students ' scores for each ranking.
The previous classmate asked me a question, how do I return the position of an element in a numeric vector that satisfies a condition in a vector? For example, X<-C (97,93,85,74,32,100,99,67) wants to return an x that satisfies the subscript in vector X of an element that has a value greater than 50 and less than 90. Thought at the time, did not think there is a good way to use the more cumbersome statements
Sort (x,index.return=true) [[2]][sort (X,index.return=true) [[1]]<90&sort (X,index.return=true) [[1]]>50], It was later found that the return value of sort (x,index.return=true) [[2]] and order (x) was the same, and that the return value of sort (x,index.return=true) [[1]] and sort (x) was the same, The statement can therefore be simplified to order (x) [Sort (x) >50&sort (x) <90]. The following is the relevant R code:
> x
[1] 97 93 85 74 32 100 99 67
> Sort (x,index.return=true) [[2]][sort (X,index.return=true) [[1]]<90&sort (X,index.return=true) [[1]]> 50]
[1] 8 4 3
> order (x) [Sort (x) >50&sort (x) <90]
[1] 8 4 3
> Sort (x,index.return=true)
$x
[1] 32 67 74 85 93 97 99 100
$ix
[1] 5 8 4 3 2 1 7 6
> Order (x)
[1] 5 8 4 3 2 1 7 6
Perhaps there are more convenient methods, such as the R has a simple function can be directly used, but not yet found, and later encountered New Bank again request guidance!
Sort in R software: sort (), rank (), order ()