Homework 1: Working with Data
Introduction
In this assignment you will practice working with a data set on housing prices in Ames, Iowa. The data dictionary can be found at https://cmustatistics.github.io/data-repository/money/ames-housing.html. You will inspect data frames, create one-way tables, use logical expressions to subset data, work with factors, and create frequency plots.
The first code chunk to load the necessary packages and data has been set up for you.
Working with categorical data
- Use
names()to inspect the variables in the Ames data set. Then useclass()to identify and report the data type for the following variables:MS.Zoning,price,Yr.SoldandBldg.Type.
- Choose two categorical variables from
ames. For each variable, create a one-waytable()and interpret one or two numbers from each.
- Let’s investigate what types of houses have central air. Subset the
amesdata set to only keep records whereCentral.Air == "Y". Save the results in a new data frame calledames_air. Then use the base pipe|>to create a one-way table ofames_air$House.Style. Write a one-sentence summary of your findings.
- Let’s investigate house styles among single family homes. Subset the
amesdata set to only keep records whereBldg.Type == "1Fam". Save the results in a new data frame calledames_1fam. Then use the base pipe|>to pipeames_1fam$House.Styleintoplot_frq(). Write a one-sentence summary of your findings that include both the frequency and percent in your answer.
Working with factors
- Use
levels()to inspect the factor levels for theBldg.Typevariable.
- Use
fct_count()to count the levels ofBldg.Type. Then usetable()on the same variable. Briefly compare the two outputs.
- The
Central.Airvariable has two levels,NandY. Usefct_recode()to create a new variable calledcentral_air_fctthat changesNto"No central air"andYto"Has central air". Verify your work with a table, then write one sentence interpreting the table.
- The
Bldg.Typevariable separates townhouse properties into two categories. Usefct_collapse()to create a simpler factor variable calledbldg_type_collapsedwith the following categories where the value on the left is the new factor level name, the values on the right the old factor level names.
-
"One-family":1Fam -
"Two-family/conversion":2fmCon -
"Duplex":Duplex -
"Townhouse":Twnhs,TwnhsE
- Verify the collapsed factor using a two-way table of the original factor and the new factor. This table should show how each original building type maps onto the new collapsed categories, and include columns to show any
NAvalues
- Factors can remember levels that are not actually present in a subsetted data frame. Create a new data frame that only includes townhouse properties from the collapsed building-type factor. Inspect the levels of the collapsed factor in this subset, then use
fct_drop()to remove unused levels and inspect the levels again.
