Other basic functions
sort()
This function sorts(orders) a vector in ascending or descending order.
# sort(vector, decreasing=F) sorts a vector from smallest to largest
sort(data$score1) # returns a sorted version of 'score1' variable (vector)
## [1] 14 17 23 23 25 27 27 31 31 32 32 33 34 35 35 36 39 40 45 51
# To sort the vector from largest to smallest, include 'decreasing=T'
sort(data$score1, decreasing = T)
## [1] 51 45 40 39 36 35 35 34 33 32 32 31 31 27 27 25 23 23 17 14
Note that ‘score1’ variable in ‘data’ object remains unsorted as we did not assign the sorted vector.
$score1 data
## [1] 35 23 14 17 23 35 27 33 32 31 34 27 51 36 39 45 31 40 25 32
rank()
This function returns a vector of the ranks of the values in a vector.
# rank(vector)
# For example, the lowest value would have a rank of 1 and the second-lowest value would have a rank of 2
rank(data$score1) # returns the ranks of the values of 'score1' variable
## [1] 14.5 3.5 1.0 2.0 3.5 14.5 6.5 12.0 10.5 8.5 13.0 6.5 20.0 16.0 17.0
## [16] 19.0 8.5 18.0 5.0 10.5
# Note that the default option for dealing with ties is to use the 'average' value
order()
This function returns the sorting index (an integer vector containing the permutation to sort a vector into ascending or descending order)
# order(vector, decreasing=F)
order(data$score1) # returns index to sort 'score1' variable in 'ascending' order
## [1] 3 4 2 5 19 7 12 10 17 9 20 8 11 1 6 14 15 18 16 13
# To get an index to sort 'score1' variable in 'descending' order:
order(data$score1, decreasing = T)
## [1] 13 16 18 15 14 1 6 11 8 9 20 10 17 7 12 19 2 5 4 3
Using [ ] and order() functions together, we can reorder data.
# vector[order(vector)] arranges a vector in ascending or descending order
<- data$score1 # a vector
score1 <- score1[order(score1, decreasing=T)] # reorder 'score1' vector from largest to smallest
reorder.sc1 reorder.sc1
## [1] 51 45 40 39 36 35 35 34 33 32 32 31 31 27 27 25 23 23 17 14
# This is actually equivalent to :
sort(data$score1, decreasing=T) # sort/order a vector from largest to smallest
## [1] 51 45 40 39 36 35 35 34 33 32 32 31 31 27 27 25 23 23 17 14
In a similar way, we can rearrange a dataframe (or matrix) by a specific variable in its ascending or descending order.
# data[order(vector), ] reorders 'rows' by 'vector' in ascending or descending order
<- data[order(data$score1, decreasing=T), ] # reorder 'data' by 'score1' in descending order
data.new data.new
## ID group score1 score2
## 13 13 2 51 45
## 16 16 2 45 41
## 18 18 2 40 17
## 15 15 2 39 37
## 14 14 2 36 26
## 1 1 1 35 45
## 6 6 1 35 47
## 11 11 2 34 48
## 8 8 1 33 50
## 9 9 1 32 15
## 20 20 2 32 27
## 10 10 1 31 37
## 17 17 2 31 25
## 7 7 1 27 37
## 12 12 2 27 16
## 19 19 2 25 15
## 2 2 1 23 14
## 5 5 1 23 27
## 4 4 1 17 25
## 3 3 1 14 26