The power of sorting enables us to view data in an arrangement so that we can analyze the data more efficiently. In a database, we can use an order by
clause to sort data with appointed columns. In R, we can use the order
and sort
functions to place data in an arrangement.
Refer to the Converting data types recipe and convert each attribute of imported data into the proper data type. Also, rename the columns of the employees
and salaries
datasets by following the steps from the Renaming the data variable recipe.
Perform the following steps to sort the salaries
dataset:
First, we can use the
sort
function to sort data:> a <- c(5,1,4,3,2,6,3) > sort(a) [1] 1 2 3 3 4 5 6 > sort(a, decreasing=TRUE) [1] 6 5 4 3 3 2 1
Next, we can determine how the
order
function works on the same input vector:> order(a) [1] 2 5 4 7 3 1 6 > order(a, decreasing = TRUE) [1] 6 1 3 4 7 5 2
To sort a data frame by a specific column, we first obtain the ordered...