How to Find Minimum or Maximum Values for Functions in R
How to Find Help Using R Mailing Lists
How to Store and Calculate Values in R

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)
 [1]  365  579 2110 2212 2541 3100 3615 4931 8277
[10] 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)
 [1] 21198 8277 4931 3615 3100 2541 2212 2110  579
[10]  365

You can access the Help documentation for the sort() function by typing ?sort into the R console.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Model Linear Data Relations with R
How to Test Data Normality Graphically in R
How to Melt Data to Long Format in R
How to Search for Individual Words in R
How to Save Graphics to an Image File in R