**R-exercises**, and kindly contributed to R-bloggers)

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

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...