0
0
R Programmingprogramming~20 mins

arrange() for sorting in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
arrange() Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
name score
1 Bob 80
2 Bob 85
3 Anna 90
4 Anna 95
B
name score
1 Anna 95
2 Anna 90
3 Bob 85
4 Bob 80
C
name score
1 Anna 90
2 Anna 95
3 Bob 80
4 Bob 85
D
name score
1 Bob 85
2 Bob 80
3 Anna 95
4 Anna 90
Attempts:
2 left
💡 Hint
Remember that arrange() sorts by the first column ascending, then by the second column descending.
🧠 Conceptual
intermediate
1:00remaining
Understanding arrange() default sorting order
By default, how does arrange() sort the columns in R's dplyr package?
AIt does not sort unless specified explicitly.
BIt sorts columns in descending order by default.
CIt sorts columns randomly by default.
DIt sorts columns in ascending order by default.
Attempts:
2 left
💡 Hint
Think about the default behavior when no desc() is used.
Predict Output
advanced
2: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)
A
x
1 3
2 2
3 1
4 NA
B
x
1 NA
2 1
3 2
4 3
C
x
1 1
2 2
3 3
4 NA
D
x
1 1
2 2
3 NA
4 3
Attempts:
2 left
💡 Hint
arrange() puts NA values at the end by default when sorting ascending.
🔧 Debug
advanced
2: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)
AError: Column `c(a, b)` not found
BError: object 'c' not found
CError: unexpected ',' in 'arrange(df, c('
DNo error, prints sorted dataframe by columns a and b
Attempts:
2 left
💡 Hint
arrange() expects columns as separate arguments, not combined in c().
🚀 Application
expert
3: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)
ARows sorted globally by descending score: 20, 15, 10, 5
BRows sorted within groups by descending score: group X (20, 10), group Y (15, 5)
CRows sorted within groups by ascending score: group X (10, 20), group Y (5, 15)
DRows remain in original order because arrange() does not work on grouped data
Attempts:
2 left
💡 Hint
arrange() sorts the entire dataframe ignoring groups unless combined with summarise or mutate.