📖 Scenario: You work in a small bookstore and keep track of book sales in a data frame. You want to organize the sales data to see which books sold the most and which sold the least.
🎯 Goal: Learn how to use the arrange() function from the dplyr package in R to sort a data frame by one or more columns.
📋 What You'll Learn
Create a data frame called sales with book titles and number of copies sold
Load the dplyr package using library(dplyr)
Use arrange() to sort the data frame by copies sold in ascending order
Print the sorted data frame
💡 Why This Matters
🌍 Real World
Sorting sales data helps bookstores understand which books are popular and manage inventory better.
💼 Career
Data analysts and business professionals often sort data frames to organize and analyze data efficiently.
Progress0 / 4 steps
1
Create the sales data frame
Create a data frame called sales with two columns: book and copies_sold. Use these exact entries: 'The Hobbit' with 150, '1984' with 200, 'Pride and Prejudice' with 120, 'To Kill a Mockingbird' with 180.
R Programming
Hint
Use data.frame() with two vectors named book and copies_sold.
2
Load the dplyr package
Write the code to load the dplyr package using library(dplyr).
R Programming
Hint
Use library(dplyr) to load the package before using arrange().
3
Sort the sales data frame by copies sold
Use the arrange() function to sort the sales data frame by the copies_sold column in ascending order. Save the result in a new variable called sorted_sales.
R Programming
Hint
Use arrange(sales, copies_sold) and assign it to sorted_sales.
4
Print the sorted data frame
Print the sorted_sales data frame to see the books sorted by copies sold in ascending order.
R Programming
Hint
Use print(sorted_sales) to display the sorted data frame.
Practice
(1/5)
1. What does the arrange() function do in R's dplyr package?
easy
A. Adds new columns to a data frame
B. Filters rows based on a condition
C. Removes duplicate rows from a data frame
D. Sorts rows of a data frame by specified columns
Solution
Step 1: Understand the purpose of arrange()
The arrange() function is used to reorder rows in a data frame based on one or more columns.
Step 2: Compare with other functions
Functions like filter() select rows, mutate() adds columns, and distinct() removes duplicates, so they are different.
Final Answer:
Sorts rows of a data frame by specified columns -> Option D
Quick Check:
arrange() = Sort rows [OK]
Hint: arrange() always changes row order by columns [OK]
Common Mistakes:
Confusing arrange() with filter()
Thinking arrange() adds or removes columns
Assuming arrange() removes duplicates
2. Which of the following is the correct syntax to sort a data frame df by column age in ascending order using arrange()?
easy
A. arrange(df, age)
B. arrange(age, df)
C. arrange(df$age)
D. arrange(df, desc(age))
Solution
Step 1: Recall arrange() syntax
The correct syntax is arrange(data_frame, column_name) to sort by that column ascending.
Step 2: Check each option
arrange(df, age) uses arrange(df, age) which is correct. arrange(age, df) reverses arguments, which is wrong. arrange(df$age) passes a vector, not a data frame. arrange(df, desc(age)) sorts descending, not ascending.
Final Answer:
arrange(df, age) -> Option A
Quick Check:
arrange(data, column) = correct syntax [OK]
Hint: First argument is data frame, second is column name [OK]
Common Mistakes:
Swapping data frame and column arguments
Passing column vector instead of data frame
Using desc() for ascending sort
3. Given the data frame df <- data.frame(name = c('Anna', 'Bob', 'Cara'), score = c(88, 95, 90)), what is the output of arrange(df, desc(score))?
medium
A. Rows sorted by score ascending: Anna, Cara, Bob
B. Rows sorted by score descending: Bob, Cara, Anna
C. Rows sorted by name alphabetically: Anna, Bob, Cara
D. Error because desc() cannot be used inside arrange()
Solution
Step 1: Understand desc() inside arrange()
Using desc(score) sorts the data frame by score in descending order (highest first).
Step 2: Apply sorting to given data
Scores are Anna=88, Bob=95, Cara=90. Descending order is Bob (95), Cara (90), Anna (88).
Final Answer:
Rows sorted by score descending: Bob, Cara, Anna -> Option B
Quick Check:
desc() sorts descending = Bob first [OK]
Hint: desc() reverses sort order inside arrange() [OK]
Common Mistakes:
Thinking desc() sorts ascending
Confusing sorting by name vs score
Believing desc() causes error
4. What is wrong with this code snippet?
arrange(df, desc(age))
where df has no column named age.
medium
A. It will cause an error because age column does not exist
B. It will ignore desc() and sort ascending
C. It will sort by another column automatically
D. It will sort rows by age as NA values
Solution
Step 1: Check column existence in arrange()
arrange() requires the column to exist in the data frame to sort by it.
Step 2: Behavior when column missing
If the column age is missing, R will throw an error indicating the column is not found.
Final Answer:
It will cause an error because age column does not exist -> Option A
5. You have a data frame df with columns city and temperature. How do you sort df first by city alphabetically ascending, then by temperature descending within each city?
hard
A. arrange(df, desc(city), temperature)
B. arrange(df, desc(city), desc(temperature))
C. arrange(df, city, desc(temperature))
D. arrange(df, temperature, city)
Solution
Step 1: Understand multi-column sorting with arrange()
arrange() sorts by columns in the order given. First column sorts primary, second column breaks ties.
Step 2: Apply sorting order to columns
We want city ascending (default) and temperature descending, so use desc(temperature) after city.
Final Answer:
arrange(df, city, desc(temperature)) -> Option C
Quick Check:
Primary city asc, secondary temp desc = arrange(df, city, desc(temperature)) [OK]
Hint: List columns in order; use desc() for descending columns [OK]
Common Mistakes:
Reversing city sorting with desc() incorrectly
Sorting temperature ascending instead of descending
Changing order of columns in arrange()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.