Bird
Raised Fist0
R Programmingprogramming~20 mins

Sorting with order() in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Order Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this R code using order()?

Consider the following R code that sorts a vector using order(). What will be the output?

R Programming
x <- c(5, 2, 9, 1)
sorted_x <- x[order(x)]
sorted_x
A[1] 9 5 2 1
B[1] 5 2 9 1
C[1] 1 2 5 9
D[1] 2 1 5 9
Attempts:
2 left
💡 Hint

The order() function returns the indices that would sort the vector.

❓ Predict Output
intermediate
2:00remaining
What is the output when ordering a data frame by two columns?

Given this data frame, what will be the output of ordering by age then score?

R Programming
df <- data.frame(name=c('Ann', 'Bob', 'Cara', 'Dan'), age=c(25, 25, 22, 25), score=c(90, 85, 88, 85))
ordered_df <- df[order(df$age, df$score), ]
ordered_df$name
A[1] "Cara" "Ann" "Bob" "Dan"
B[1] "Cara" "Bob" "Dan" "Ann"
C[1] "Dan" "Bob" "Ann" "Cara"
D[1] "Bob" "Ann" "Dan" "Cara"
Attempts:
2 left
💡 Hint

First sort by age ascending, then by score ascending.

🔧 Debug
advanced
2:00remaining
Why does this order() call produce an error?

Examine the code below. Why does it produce an error?

R Programming
x <- c(3, 1, 4)
order(x > 2, x)
ANo error; returns indices ordering by x > 2 then x
BError because order() expects vectors of the same length and type
CError because logical vector cannot be used directly with numeric vector in order()
DError because > operator is not vectorized
Attempts:
2 left
💡 Hint

Check if logical vectors can be used in order().

📝 Syntax
advanced
2:00remaining
Which option has correct syntax to sort a vector in descending order using order()?

Choose the correct R code to sort vector v in descending order using order().

R Programming
v <- c(10, 5, 8, 3)
Av[order(-1 * v)]
Bv[order(v, decreasing=TRUE)]
Cv[order(v, desc=TRUE)]
Dv[order(-v)]
Attempts:
2 left
💡 Hint

Remember order() does not have a decreasing argument; use negative values to reverse order.

🚀 Application
expert
2:00remaining
How many items are in the sorted vector after removing duplicates?

Given the vector v <- c(4, 2, 4, 3, 2, 1), how many items are in the vector after sorting with order() and removing duplicates?

R Programming
v <- c(4, 2, 4, 3, 2, 1)
unique_sorted_v <- unique(v[order(v)])
length(unique_sorted_v)
A4
B5
C6
D3
Attempts:
2 left
💡 Hint

First sort the vector, then remove duplicates, then count the length.

Practice

(1/5)
1. What does the order() function in R do?
easy
A. Sorts the vector and returns the sorted values directly
B. Removes duplicates from a vector
C. Returns the indices that would sort a vector in ascending order
D. Calculates the sum of all elements in a vector

Solution

  1. Step 1: Understand the purpose of order()

    The order() function returns the positions (indices) that would sort the vector, not the sorted vector itself.
  2. Step 2: Differentiate from sort()

    Unlike sort(), which returns sorted values, order() returns the order of indices to rearrange the vector.
  3. Final Answer:

    Returns the indices that would sort a vector in ascending order -> Option C
  4. Quick Check:

    order() = indices for sorting [OK]
Hint: Remember: order() gives positions, sort() gives sorted values [OK]
Common Mistakes:
  • Confusing order() with sort()
  • Expecting order() to return sorted values
  • Using order() to remove duplicates
2. Which of the following is the correct syntax to sort a vector v in decreasing order using order()?
easy
A. v[sort(v, decreasing = TRUE)]
B. order(v, decreasing = TRUE)
C. sort(order(v), decreasing = TRUE)
D. v[order(v, decreasing = TRUE)]

Solution

  1. Step 1: Use order() with decreasing = TRUE

    To sort vector v in decreasing order, use order(v, decreasing = TRUE) to get indices for sorting from largest to smallest.
  2. Step 2: Index vector v with order()

    Use these indices to reorder v as v[order(v, decreasing = TRUE)].
  3. Final Answer:

    v[order(v, decreasing = TRUE)] -> Option D
  4. Quick Check:

    v[order(v, decreasing=TRUE)] sorts descending [OK]
Hint: Index vector with order() for sorting, add decreasing=TRUE for reverse [OK]
Common Mistakes:
  • Using order() without indexing the vector
  • Trying to sort order() output directly
  • Using sort() on order() output incorrectly
3. What is the output of the following R code?
v <- c(4, 2, 9, 7)
v[order(v)]
medium
A. [1] 2 4 7 9
B. [1] 4 2 9 7
C. [1] 9 7 4 2
D. [1] 2 7 4 9

Solution

  1. Step 1: Calculate order(v)

    For v = c(4, 2, 9, 7), order(v) returns indices that sort v: positions of values in ascending order are 2 (2), 1 (4), 4 (7), 3 (9).
  2. Step 2: Index v with order(v)

    Using these indices: v[c(2,1,4,3)] gives 2 4 7 9.
  3. Final Answer:

    [1] 2 4 7 9 -> Option A
  4. Quick Check:

    v[order(v)] = sorted vector ascending [OK]
Hint: order() gives indices to sort; index vector to get sorted values [OK]
Common Mistakes:
  • Expecting order() to return sorted values directly
  • Mixing up indices and values
  • Incorrect indexing order
4. Identify the error in the following R code snippet:
v <- c(5, 3, 8, 1)
sorted_v <- order(v)
print(sorted_v)
medium
A. order() returns indices, not sorted values; should index v with order(v)
B. Missing parentheses in order function call
C. Vector v is not defined before order()
D. print() cannot display vectors

Solution

  1. Step 1: Understand what order() returns

    order(v) returns the indices that would sort v, not the sorted vector itself.
  2. Step 2: Correct usage to get sorted vector

    To get sorted values, use v[order(v)]. The code assigns indices to sorted_v, so printing it shows positions, not sorted values.
  3. Final Answer:

    order() returns indices, not sorted values; should index v with order(v) -> Option A
  4. Quick Check:

    order() = indices, index vector to sort [OK]
Hint: Use v[order(v)] to get sorted vector, not order(v) alone [OK]
Common Mistakes:
  • Expecting order() to return sorted vector
  • Printing order() output directly
  • Confusing order() with sort()
5. Given two related vectors names <- c("Anna", "Bob", "Cara") and scores <- c(88, 95, 90), how do you sort names by scores in descending order using order()?
hard
A. order(names, decreasing = TRUE)[scores]
B. names[order(scores, decreasing = TRUE)]
C. scores[order(names, decreasing = TRUE)]
D. sort(names, by = scores, decreasing = TRUE)

Solution

  1. Step 1: Use order() on scores with decreasing = TRUE

    To sort names by scores descending, get indices with order(scores, decreasing = TRUE).
  2. Step 2: Index names with these order indices

    Use these indices to reorder names: names[order(scores, decreasing = TRUE)].
  3. Final Answer:

    names[order(scores, decreasing = TRUE)] -> Option B
  4. Quick Check:

    Index names by order(scores, decreasing=TRUE) [OK]
Hint: Sort related vectors by ordering one, index the other [OK]
Common Mistakes:
  • Trying to sort names directly by scores without order()
  • Using sort() with non-existent 'by' argument
  • Mixing order() arguments or indexing incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard