Session Ready
Exercise

order

The function order() returns the index vector needed to sort the vector. This implies that sort(x) and x[order(x)] give the same result.

This can be useful for finding row numbers with certain properties such as "the row for the state with the smallest population". Remember that when we extract a variable from a data frame the order of the resulting vector is the same as the order of the rows of the data frame. So for example, the entries of the vector murders$state are ordered in the same way as the states if you go down the rows of murders.

Instructions
100 XP
  • Now instead of the smallest population size, let's find out the row number, in the data frame murders, of the state with the smallest population size.
  • This time we need to use order() instead of sort().
  • Remember that the entries in the vector murders$population follow the order of the rows of murders.