Introduction
The arrange() function sorts data frames by one or more columns. It's essential for data organization.
Basic Sorting
library(dplyr)
df <- tibble(
name = c("Charlie", "Alice", "Bob"),
score = c(78, 85, 90),
age = c(35, 25, 30)
)
# Sort by single column
arrange(df, score)
# Sort descending
arrange(df, desc(score))
Multiple Columns
# Sort by multiple columns
arrange(df, age, score)
# Different directions
arrange(df, desc(age), score)
Using Pipes
df %>%
filter(score > 80) %>%
arrange(desc(score)) %>%
select(name, score)
Summary
arrange() organizes data by sorting. Use desc() for descending order and multiple columns for complex sorting.