R Filter and Sort Data Frames
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 »