Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In the exercises below we cover the basics of ordering vectors, matrices and data frames. We consider both column-wise and row-wise ordering, single and multiple variables, ascending and descending sorting, and sorting based on numeric, character and factor variables. Before proceeding, it might be helpful to look over the help pages for the sort, order, and xtfrm functions.

Answers to the exercises are available here.

If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

Exercise 1
Sort the vector x <- c(1, 3, 2, 5, 4) in:
a. ascending order
b. descending order

Exercise 2
Sort the matrix x <- matrix(1:100, ncol=10):
a. in descending order by its second column (call the sorted matrix x1)
b. in descending order by its second row (call the sorted matrix x2)

Exercise 3
Sort only the first column of x in descending order.

Exercise 4
Consider the women data.
a. Confirm that the data are sorted in increasing order for both the height and weight variable, without looking at the data.
b. Create a new variable bmi, based on the following equation: BMI = ( Weight in Pounds / (Height in inches) x (Height in inches) ) x 703. Check, again without looking at the data, whether BMI increases monotonically with weight and height.
c. Sort the dataframe on bmi, and its variable names alphabetically

Exercise 5
Consider the CO2 data.
a. Sort the data based on the Plant variable, alphabetically. (Note that Plant is a factor!). Check that the data are sorted correctly by printing the data on the screen.
b. Sort the data based on the uptake (increasing) and Plant (alphabetically) variables (in that order).
c. Sort again, based on uptake (increasing) and Plant (reversed alphabetically), in that order.

Exercise 6
Create a dataframe df with 40 columns, as follows:
df <- as.data.frame(matrix(sample(1:5, 2000, T), ncol=40))
a. Sort the dataframe on all 40 columns, from left to right, in increasing order.
a. Sort the dataframe on all 40 columns, from left to right, in decreasing order.
c. Sort the dataframe on all 40 columns, from right to left, in increasing order.