Data Frames, Ranks and Orders
This exercise is somewhat more challenging. We are going to
repeat the previous exercise but this time order my_df
so that the states are ordered from least populous to most.
This exercise is part of the course
Data Science R Basics
Exercise instructions
- Create variables
states
andranks
to store the state names and ranks by population size respectively. - Create an object
ind
that stores the indexes needed to order the population values, using theorder
command. For example we could defineo <- order(murders$population)
- Create a data frame with both variables following the correct order. Use the bracket operator
[
to re-order each column in the data frame. For example,states[o]
orders the abbreviations based by population size. - The columns of the data frame must be in the specific order and have the specific names:
states
,ranks
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define a variable states to be the state names from the murders data frame
# Define a variable ranks to determine the population size ranks
# Define a variable ind to store the indexes needed to order the population values
# Create a data frame my_df with the state name and its rank and ordered from least populous to most