Sorting data

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.

Getting ready

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.

How to do it…

Perform the following steps to sort the salaries dataset:

  1. 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 ...

Get R for Data Science Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.