Sort data frames by columns (2024)

[This article was first published on Quantargo Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Sort data frames by columns (1)

To select areas of interest in a data frame they often need to be ordered by specific columns. The dplyr arrange() function supports data frame orderings by multiple columns in ascending and descending order.

  • Use the arrange() function to sort data frames.
  • Sort data frames by multiple columns using arrange().
arrange(, )arrange(, , , ...)

The arrange() function with a single column

arrange(, )arrange(, , , ...)

The arrange() function orders the rows of a data frame. It takes a data frame or a tibble as the first parameter and the names of the columns based on which the rows should be ordered as additional parameters. Let’s assume, we want to answer the question: Which states had the highest percentage of Republican voters in the 2016 US presidential election? To answer this question, in the following example we use the pres_results_2016 data frame, containing information only for the 2016 US presidential election. We arrange() the data frame based on the rep column (Republican votes in percentage):

arrange(pres_results_2016, rep)# A tibble: 51 x 6 year state total_votes dem rep other <dbl> <chr> <dbl> <dbl> <dbl> <dbl>1 2016 DC 312575 0.905 0.0407 0.03352 2016 HI 437664 0.610 0.294 0.09583 2016 VT 320467 0.557 0.298 0.0737# … with 48 more rows

As you can see in the output, the data frame is sorted in an ascending order based on the rep column. However, we would prefer to have the results in a descending order, so that we can instantly see the state with the highest rep percentage. To sort a column in a descending order, all we need to do is apply the desc() function on the given column inside the arrange() function:

arrange(pres_results_2016, desc(rep))# A tibble: 51 x 6 year state total_votes dem rep other <dbl> <chr> <dbl> <dbl> <dbl> <dbl>1 2016 WV 713051 0.265 0.686 0.04892 2016 WY 258788 0.216 0.674 0.08303 2016 OK 1452992 0.289 0.653 0.0575# … with 48 more rows

Arranging is not only possible on numeric values, but on character values as well. In that case, dplyr sorts the rows in alphabetic order. We can arrange character columns just like numeric ones:

arrange(pres_results_2016, state)# A tibble: 51 x 6 year state total_votes dem rep other <dbl> <chr> <dbl> <dbl> <dbl> <dbl>1 2016 AK 318608 0.366 0.513 0.09282 2016 AL 2123372 0.344 0.621 0.02543 2016 AR 1130635 0.337 0.606 0.0577# … with 48 more rows

Exercise: Use arrange() based on a single column

The gapminder_2007 dataset contains economic and demographic data about various countries for the year 2007. Arrange the tibble and inspect which country had the lowest life expectancy lifeExp in 2007! The dplyr package is already loaded.

  1. Apply the arrange() function on the gapminder_2007 tibble
  2. Order the tibble based on the lifeExp column

Start Exercise

Exercise: Use arrange() in combination with desc()

The gapminder_2007 dataset contains economic and demographic data about various countries for the year 2007. Arrange the tibble and inspect which countries had the largest population in 2007! The dplyr package is already loaded.

  1. Apply the arrange() function on the gapminder_2007 tibble.
  2. Sort the tibble in a descending order based on the pop column.

Start Exercise

The arrange() function with multiple columns

We can use the arrange() function on multiple columns as well. In this case the order of the columns in the function parameters, sets a hierarchy of ordering. The function starts by ordering the rows based on the first column defined in the parameters. In case there are several rows with the same value, the function decides the order based on the second column defined in the parameters. If there are still multiple rows with the same values, the function decides based on the third column defined in the parameters (if defined) and so on.

In the following example we use the pres_results_subset data frame, containing election results only for the states: "TX"(Texas),"UT"(Utah) and "FL"(Florida). First we sort the data frame in a descending order based on the year column. Then, we add a second level, and order the data frame based on the dem column:

arrange(pres_results_subset, year, dem)# A tibble: 33 x 6 year state total_votes dem rep other <dbl> <chr> <dbl> <dbl> <dbl> <dbl>1 1976 UT 541218 0.336 0.624 0.0392 2 1976 TX 4071884 0.511 0.480 0.008173 1976 FL 3150631 0.519 0.466 0.0143 # … with 30 more rows

As you can see in the output, the data frame is overall ordered based on the year column. However, when the value of year is the same, the order of the rows is decided by the dem column.

Exercise: Use arrange() based on multiple columns

The gapminder_2007 tibble contains economic and demographic data about various countries for the year 2007. Arrange the tibble and inspect for each continent, which countries had the highest life expectancy in 2007! The dplyr package is already loaded.

  1. Apply the arrange() function on the gapminder_2007 tibble.
  2. Order the tibble based on the continent column!
  3. In case there are rows with the same continent, sort the tibble in a descending order based on the lifeExp column!

Start Exercise

Quiz: arrange() Function

Which of the following statements are true about the arrange() function?

  • The arrange() function orders the rows of a data frame.
  • To arrange() the values of column in an ascending order, we need to use the asc() function.
  • To arrange() the values of column in a descending order, we need to use the desc() function.
  • You can only arrange() a data frame based on one column.

Start Quiz

Sort data frames by columns is an excerpt from the course Introduction to R, which is available for free at quantargo.com

VIEW FULL COURSE

Related

To leave a comment for the author, please follow the link and comment on their blog: Quantargo Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Sort data frames by columns (2024)
Top Articles
Beyond Einstein: non-local physics, 6 th ed
Wegmans Hours - Open/ Closed | Pharmacy, Sub Shop, Holiday Hours
Cpmc Mission Bernal Campus & Orthopedic Institute Photos
Canya 7 Drawer Dresser
Access-A-Ride – ACCESS NYC
80 For Brady Showtimes Near Marcus Point Cinema
Steve Strange - From Punk To New Romantic
Craigslistdaytona
Tripadvisor Near Me
R Tiktoksweets
Guardians Of The Galaxy Vol 3 Full Movie 123Movies
WWE-Heldin Nikki A.S.H. verzückt Fans und Kollegen
People Portal Loma Linda
Me Cojo A Mama Borracha
Christina Steele And Nathaniel Hadley Novel
CVS Near Me | Columbus, NE
Poe Str Stacking
Spn 520211
Theater X Orange Heights Florida
R. Kelly Net Worth 2024: The King Of R&B's Rise And Fall
3569 Vineyard Ave NE, Grand Rapids, MI 49525 - MLS 24048144 - Coldwell Banker
Violent Night Showtimes Near Johnstown Movieplex
Mikayla Campinos: Unveiling The Truth Behind The Leaked Content
Copper Pint Chaska
'Insidious: The Red Door': Release Date, Cast, Trailer, and What to Expect
Creed 3 Showtimes Near Island 16 Cinema De Lux
Jamielizzz Leaked
Bursar.okstate.edu
Jeep Cherokee For Sale By Owner Craigslist
Emiri's Adventures
First Light Tomorrow Morning
Ixlggusd
Metra Union Pacific West Schedule
Kips Sunshine Kwik Lube
Metra Schedule Ravinia To Chicago
Myfxbook Historical Data
R Nba Fantasy
Wasmo Link Telegram
Isabella Duan Ahn Stanford
Free Crossword Puzzles | BestCrosswords.com
Brake Pads - The Best Front and Rear Brake Pads for Cars, Trucks & SUVs | AutoZone
Makes A Successful Catch Maybe Crossword Clue
Paperlessemployee/Dollartree
A Man Called Otto Showtimes Near Cinemark Greeley Mall
Call2Recycle Sites At The Home Depot
Uno Grade Scale
Where Is Darla-Jean Stanton Now
Att Corporate Store Location
Laurel Hubbard’s Olympic dream dies under the world’s gaze
Syrie Funeral Home Obituary
Fetllife Com
Unity Webgl Extreme Race
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5620

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.