The order() function returns the indices that would sort a vector or data frame. You can use these indices to rearrange data in ascending or descending order.
Click to reveal answer
beginner
How do you sort a vector ascending using order()?
Use vec[order(vec)]. This returns the vector elements sorted from smallest to largest.
Click to reveal answer
intermediate
How can you sort a data frame by multiple columns using order()?
Use df[order(df$col1, df$col2), ]. This sorts first by col1, then by col2 within ties.
Click to reveal answer
intermediate
How do you sort in descending order with order()?
Use order(-vec) for numeric vectors. For characters, use order(vec, decreasing = TRUE).
Click to reveal answer
intermediate
Why use order() instead of sort()?
order() gives the positions to rearrange data, useful for sorting data frames or multiple vectors together. sort() just returns a sorted vector.
Click to reveal answer
What does order(c(3,1,2)) return?
A[2, 1, 3]
B[2, 3, 1]
C[3, 1, 2]
D[1, 2, 3]
✗ Incorrect
The order function returns indices that sort the vector. The smallest element is at position 2 (value 1), then position 3 (value 2), then position 1 (value 3).
How do you sort a data frame df by column age ascending?
Adf[order(df$age), ]
Border(df$age)
Csort(df$age)
Ddf[sort(df$age), ]
✗ Incorrect
You use order to get the sorting indices and then reorder the rows of df accordingly.
Which code sorts a vector v in descending order?
Av[order(v)]
Bsort(v)
Cv[order(-v)]
Dorder(v, decreasing=TRUE)
✗ Incorrect
Using negative values with order sorts numeric vectors in descending order.
What is the main difference between sort() and order()?
Asort returns sorted values; order returns sorting indices
Border returns sorted values; sort returns indices
CBoth return sorted values
DBoth return sorting indices
✗ Incorrect
sort() returns the sorted vector, while order() returns the positions to sort the vector.
How do you sort a data frame by two columns, name then score?
Asort(df$name, df$score)
Bdf[order(df$score, df$name), ]
Corder(df$name, df$score)
Ddf[order(df$name, df$score), ]
✗ Incorrect
You pass multiple columns to order to sort by the first, then the second within ties.
Explain how the order() function works and how it differs from sort().
Think about what you get back from each function and how you use it.
You got /4 concepts.
Describe how to sort a data frame by multiple columns using order().
Remember how to handle ties in sorting.
You got /3 concepts.
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
Step 1: Understand the purpose of order()
The order() function returns the positions (indices) that would sort the vector, not the sorted vector itself.
Step 2: Differentiate from sort()
Unlike sort(), which returns sorted values, order() returns the order of indices to rearrange the vector.
Final Answer:
Returns the indices that would sort a vector in ascending order -> Option C
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
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).
Step 2: Index v with order(v)
Using these indices: v[c(2,1,4,3)] gives 2 4 7 9.
Final Answer:
[1] 2 4 7 9 -> Option A
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
Step 1: Understand what order() returns
order(v) returns the indices that would sort v, not the sorted vector itself.
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.
Final Answer:
order() returns indices, not sorted values; should index v with order(v) -> Option A
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
Step 1: Use order() on scores with decreasing = TRUE
To sort names by scores descending, get indices with order(scores, decreasing = TRUE).
Step 2: Index names with these order indices
Use these indices to reorder names: names[order(scores, decreasing = TRUE)].
Final Answer:
names[order(scores, decreasing = TRUE)] -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.