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)
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
.
Order the dataframe my_df
from smallest to largest population. Call the data frame my_df_ordered
and its 2 columns respectively state
and rank
.
Hint:
create an object
ind
using theorder
function that stores the indexes of the rows sorted by their corresponding population values. Then use the bracket operator ([
) to re-order the rows of the dataframe.