For these exercises we will use the US murders dataset. Make sure you load it prior to starting.

library(dslabs)
data("murders")

Exercise

  1. Find the smallest population size reported in the population column, store the result in pop_min. We expect you to use the following method:
    • extract the population column from the dataframe using the $ operator
    • sort the resulting vector using the sort function
    • select and store the first (and thus smallest) population size using the [ operator

    Pitfall

    Like every function in R the sort function does not alter it’s parameters (the vector to sort), it only returns a new sorted vector. If you don’t store the returned vector or override the unsorted vector, your sorted vector will be lost. (e.g. sorted <- sort(original))

  2. Now instead of the smallest population size, find the index (=rownumber) of the entry with the smallest population size in the unsorted population size data. store the index in pop_min_index.

    Hint

    You can use the same method described in question 1 using the order function instead of sort. As explained in 2.10 Sorting order will first sort the values and afterwards replace each value with it’s original index (index before sorting)

  3. We can actually perform the same operation as in the previous exercise (question 2) one single function which.min. Write one line of code that does this. Store your result in pop_min_index_wm.

  4. Now we know how small the smallest state is and we know which row represents it. Which state is it? extract the state names column and store the state with the smallest population in smallest_state.