3.4 Exercises
For these exercises we will use the us_states
and us_states_df
datasets from the spData package:
library(spData)
data(us_states)
data(us_states_df)
us_states
is a spatial object (of class sf
), containing geometry and a few attributes (including name, region, area, and population) of states within the contiguous United States.us_states_df
is a data frame (of class data.frame
) containing the name and additional variables (including median income and poverty level, for the years 2010 and 2015) of US states, including Alaska, Hawaii and Puerto Rico.The data comes from the United States Census Bureau, and is documented in ?us_states
and ?us_states_df
.
- Create a new object called
us_states_name
that contains only theNAME
column from theus_states
object.What is the class of the new object and what makes it geographic? - Select columns from the
us_states
object which contain population data.Obtain the same result using a different command (bonus: try to find three ways of obtaining the same result).Hint: try to use helper functions, such ascontains
orstarts_with
from dplyr (see?contains
). - Find all states with the following characteristics (bonus find and plot them):
- Belong to the Midwest region.
- Belong to the West region, have an area below 250,000 km2and in 2015 a population greater than 5,000,000 residents (hint: you may need to use the function
units::set_units()
oras.numeric()
). - Belong to the South region, had an area larger than 150,000 km2 or a total population in 2015 larger than 7,000,000 residents.
- What was the total population in 2015 in the
us_states
dataset?What was the minimum and maximum total population in 2015? - How many states are there in each region?
- What was the minimum and maximum total population in 2015 in each region?What was the total population in 2015 in each region?
- Add variables from
us_states_df
tous_states
, and create a new object calledus_states_stats
.What function did you use and why?Which variable is the key in both datasets?What is the class of the new object? us_states_df
has two more rows thanus_states
.How can you find them? (hint: try to use thedplyr::anti_join()
function)- What was the population density in 2015 in each state?What was the population density in 2010 in each state?
- How much has population density changed between 2010 and 2015 in each state?Calculate the change in percentages and map them.
- Change the columns’ names in
us_states
to lowercase. (Hint: helper functions -tolower()
andcolnames()
may help.) - Using
us_states
andus_states_df
create a new object calledus_states_sel
.The new object should have only two variables -median_income_15
andgeometry
.Change the name of themedian_income_15
column toIncome
. - Calculate the change in median income between 2010 and 2015 for each state.Bonus: What was the minimum, average and maximum median income in 2015 for each region?What is the region with the largest increase of the median income?
- Create a raster from scratch with nine rows and columns and a resolution of 0.5 decimal degrees (WGS84).Fill it with random numbers.Extract the values of the four corner cells.
- What is the most common class of our example raster
grain
(hint:modal()
)? - Plot the histogram and the boxplot of the
data(dem, package = "RQGIS")
raster.
References
Grolemund, Garrett, and Hadley Wickham. 2016. R for Data Science. O’Reilly Media.
Wickham, Hadley. 2014a. Advanced R. CRC Press.