How to Sort and Order Data in R
One very common task in data analysis and reporting is sorting information, which you can do easily in R. You can answer many everyday questions with league tables — sorted tables of data that tell you the best or worst of specific things.
For example, parents want to know which school in their area is the best, and businesses need to know the most productive factories or the most lucrative sales areas. When you have the data, you can answer all these questions simply by sorting it.
As an example, look again at the built-in data about the states in the U.S. First, create a data frame called some.states that contains information contained in the built-in variables state.region and state.x77:
> some.states <- data.frame( + Region = state.region, + state.x77)
To keep the example manageable, create a subset of only the first ten rows and the first three columns:
> some.states <- some.states[1:10, 1:3] > some.states Region Population Income Alabama South 3615 3624 Alaska West 365 6315 Arizona West 2212 4530 .... Delaware South 579 4809 Florida South 8277 4815 Georgia South 4931 4091
You now have a variable called some.states that is a data frame consisting of ten rows and three columns (Region, Population, and Income).
R makes it easy to sort vectors in either ascending or descending order. Because each column of a data frame is a vector, you may find that you perform this operation quite frequently.
How to sort a vector in ascending order
To sort a vector, you use the sort() function. For example, to sort Population in ascending order, try this:
> sort(some.states$Population)  365 579 2110 2212 2541 3100 3615 4931 8277  21198
How to sort a vector in decreasing order
You also can tell sort() to go about its business in decreasing order. To do this, specify the argument decreasing=TRUE:
> sort(some.states$Population, decreasing=TRUE)  21198 8277 4931 3615 3100 2541 2212 2110 579  365
You can access the Help documentation for the sort() function by typing ?sort into the R console.