Homework 1: Working with Data

Author

Your Name

Published

August 24, 2026

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.

Code
library(forcats)
library(sjPlot)

ames <- openintro::ames 

Working with categorical data

  1. Use names() to inspect the variables in the Ames data set. Then use class() to identify and report the data type for the following variables: MS.Zoning, price, Yr.Sold and Bldg.Type.
  1. Choose two categorical variables from ames. For each variable, create a one-way table() and interpret one or two numbers from each.
  1. Let’s investigate what types of houses have central air. Subset the ames data set to only keep records where Central.Air == "Y". Save the results in a new data frame called ames_air. Then use the base pipe |> to create a one-way table of ames_air$House.Style. Write a one-sentence summary of your findings.
  1. Let’s investigate house styles among single family homes. Subset the ames data set to only keep records where Bldg.Type == "1Fam". Save the results in a new data frame called ames_1fam. Then use the base pipe |> to pipe ames_1fam$House.Style into plot_frq(). Write a one-sentence summary of your findings that include both the frequency and percent in your answer.

Working with factors

  1. Use levels() to inspect the factor levels for the Bldg.Type variable.
  1. Use fct_count() to count the levels of Bldg.Type. Then use table() on the same variable. Briefly compare the two outputs.
  1. The Central.Air variable has two levels, N and Y. Use fct_recode() to create a new variable called central_air_fct that changes N to "No central air" and Y to "Has central air". Verify your work with a table, then write one sentence interpreting the table.
  1. The Bldg.Type variable separates townhouse properties into two categories. Use fct_collapse() to create a simpler factor variable called bldg_type_collapsed with 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
  1. 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 NA values
  1. 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.