For these exercises we will use the US murders dataset again. Make sure you load it prior to starting.
library(dslabs)
data("murders")
You can create a data frame using the data.frame
function. Here is
a quick example:
temp <- c(35, 88, 42, 84, 81, 30)
city <- c("Beijing", "Lagos", "Paris", "Rio de Janeiro",
"San Juan", "Toronto")
city_temps <- data.frame(name = city, temperature = temp)
5. Use the rank
function to determine the population rank of each state
from smallest population size to biggest. Save these ranks in an object
called ranks
, then create a data frame with the state name and its
rank. Call the data frame my_df
and its 2 column respectively state
and rank
.
6. Repeat the previous exercise, but this time order the dataframe so that
the states are ordered from least populous to most populous. Call the
data frame my_df_ordered
and its 2 column respectively state
and rank
.
Hint: create an object ind
that stores the indexes needed to order the
population values. Then use the bracket operator [
to re-order each column
in the data frame.