Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

R Filter and Sort Data Frames


Share

Filter and Sort

Real data frames often have thousands of rows. Usually you only need some of them (filter), in a certain order (sort).

In this chapter, we use this data frame with five training sessions:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

Filter Rows

In the Vector Operations chapter, you learned to filter a vector with a condition inside []. You can do the same with the rows of a data frame.

Write the condition before the comma. Leave the place after the comma empty to keep all columns:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

Data_Frame[Data_Frame$Pulse > 120, ]
Try it Yourself »

Only the rows where Pulse is greater than 120 are returned. The numbers on the left are the row numbers from the original data frame.

Use & (and) or | (or) to combine conditions:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

Data_Frame[Data_Frame$Training == "Strength" & Data_Frame$Duration > 55, ]
Try it Yourself »

The subset() Function

The subset() function does the same thing, but is easier to read. You do not need to write Data_Frame$ in front of each column name:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

subset(Data_Frame, Pulse > 120)
Try it Yourself »

Use the select argument to choose which columns to keep:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

subset(Data_Frame, Pulse > 120, select = c(Training, Pulse))
Try it Yourself »


Sort a Data Frame

The order() function returns the row positions in sorted order. Put it before the comma to sort the rows:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

Data_Frame[order(Data_Frame$Pulse), ]
Try it Yourself »

Add decreasing = TRUE to sort from highest to lowest:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

Data_Frame[order(Data_Frame$Pulse, decreasing = TRUE), ]
Try it Yourself »

Get the First Rows

The head() function returns the first rows of a data frame. Combine it with order() to find the top 3 sessions with the highest pulse:

Example

Data_Frame <- data.frame (
  Training = c("Strength", "Stamina", "Other", "Stamina", "Strength"),
  Pulse = c(100, 150, 120, 140, 110),
  Duration = c(60, 30, 45, 40, 55)
)

sorted <- Data_Frame[order(Data_Frame$Pulse, decreasing = TRUE), ]

head(sorted, 3)
Try it Yourself »


Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.