Quantcast
Channel: r software hub
Viewing all articles
Browse latest Browse all 1015

Get-your-stuff-in-order exercises

$
0
0

By r-exercises

Sorting_quicksort_anim

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 in:
a. ascending order
b. descending order

Exercise 2
Sort the matrix x :
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
a. Sort the dataframe on all 40 columns, from left to ...read more

Source:: r-bloggers.com


Viewing all articles
Browse latest Browse all 1015

Trending Articles