Challenge - 5 Problems
arrange() Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of arrange() with multiple columns
What is the output of the following R code using
dplyr::arrange()?R Programming
library(dplyr) df <- tibble(name = c("Anna", "Bob", "Anna", "Bob"), score = c(90, 85, 95, 80)) arranged <- arrange(df, name, desc(score)) print(arranged)
Attempts:
2 left
💡 Hint
Remember that arrange() sorts by the first column ascending, then by the second column descending.
✗ Incorrect
The arrange() function sorts the data frame first by name in ascending order (Anna before Bob), then by score in descending order within each name group.
🧠 Conceptual
intermediate1:00remaining
Understanding arrange() default sorting order
By default, how does
arrange() sort the columns in R's dplyr package?Attempts:
2 left
💡 Hint
Think about the default behavior when no
desc() is used.✗ Incorrect
By default, arrange() sorts columns in ascending order unless wrapped with desc() to reverse the order.
❓ Predict Output
advanced2:00remaining
arrange() with NA values
What is the output of this R code snippet using
arrange() when the data contains NA values?R Programming
library(dplyr) df <- tibble(x = c(3, NA, 1, 2)) arranged <- arrange(df, x) print(arranged)
Attempts:
2 left
💡 Hint
arrange() puts NA values at the end by default when sorting ascending.
✗ Incorrect
When sorting ascending, arrange() places NA values last.
🔧 Debug
advanced2:00remaining
Identify the error in arrange() usage
What error will this R code produce?
R Programming
library(dplyr) df <- tibble(a = c(1, 2, 3), b = c(4, 5, 6)) arranged <- arrange(df, c(a, b)) print(arranged)
Attempts:
2 left
💡 Hint
arrange() expects columns as separate arguments, not combined in c().
✗ Incorrect
The arrange() function does not accept a vector of columns using c(). It expects columns as separate arguments.
🚀 Application
expert3:00remaining
Sorting a grouped dataframe with arrange()
Given this grouped dataframe, what will be the order of rows after applying
arrange(desc(score))?R Programming
library(dplyr) df <- tibble(group = c('X', 'X', 'Y', 'Y'), score = c(10, 20, 15, 5)) grouped <- group_by(df, group) arranged <- arrange(grouped, desc(score)) print(arranged)
Attempts:
2 left
💡 Hint
arrange() sorts the entire dataframe ignoring groups unless combined with summarise or mutate.
✗ Incorrect
Even if the dataframe is grouped, arrange() sorts all rows globally by the specified column.