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.

Image: en:User:RolandH [GFDL, CC-BY-SA-3.0 or CC BY-SA 2.5-2.0-1.0], via Wikimedia Commons

Jan Trommelmans says

In Exercise 3: should the solution not have “decreasing=TRUE”?

Alternative solutions for Exercise 6:

df<-apply(df,2,sort,decreasing=FALSE)

df<-apply(df,2,sort,decreasing=TRUE)

df<-apply(rev(df),2,sort,decreasing=FALSE)

( I hope these are OK?)

Han de Vries says

Dear Jan,

Thanks for spotting the error. I just updated the solution. If you compare the resulting dataframes of your alternative solutions for Ex 6 to those shown in the solutions, you’ll notice that they differ. In your proposed solutions, you sort every column separately. So, you obtain a new dataframe with every column sorted from low to high or vice versa. This is different from what the exercise asks for, i.e. to sort the entire dataframe. Stated differently: in my solution, within a particular row, the data stays the same after sorting, except that the row ends up in a different position in the dataframe. In your solution, the data changes within each row. See also the comment by Bob Muenchen to this set: http://r-exercises.com/2016/09/08/efficient-processing-with-apply-exercises/

Sergio says

Exercise 6 is very interesting but I do not understand how do.call works in this case. Why does it do what id does?