Knowledge Quiz: Tutorial 1

Note
  • Click the check-mark button to check your answer.
  • Click the question-mark button to see an explanation of the solution.

Which command sorts the numeric vector x in descending order using only base R?

  • FALSE. sort(x) sorts in ascending order by default.
  • TRUE. sort(x, decreasing = TRUE) correctly sorts x in descending order.
  • FALSE. sort(x, reverse = TRUE) is not a valid argument in the sort function.
  • FALSE. sort(-x) sorts the negated values of x, which does not yield the correct descending order of x.
  • FALSE. order(x, decreasing = TRUE) returns the indices that would sort x in descending order, not the sorted vector itself.

Suppose you have

{. .cell-code} y <- data.frame(x1 = 1:10, x2 = c(-1, 1), x3 = runif(10))

Which of the following commands sort the rows by x3 in ascending order (same result, possibly using different syntaxes)?

  • TRUE. arrange(y, x3) sorts the data frame y by the column x3 in ascending order.
  • TRUE. arrange(y, +x3) also sorts y by x3 in ascending order; the + sign does not change the order.
  • TRUE. y[order(y$x3, decreasing = FALSE), ] sorts the rows of y by x3 in ascending order.
  • FALSE. y[order(x3), ] will result in an error because x3 is not defined in the global environment; it needs to be referenced as y$x3.
  • FALSE. arrange(y, -x3) sorts y by x3 in descending order, which is not what we want.
Back to top