AE 09: Importing Data

Application exercise

Getting started

Packages

We will use the following two packages in this application exercise.

  • tidyverse: For data import, wrangling, and visualization.
  • readxl: For importing data from Excel.

Part 1: Hollywood relationships

  1. Load the data from age-gaps.csv in your data and assign it to age_gaps. Confirm that this new object appears in your Environment tab. Click on the name of the object in your Environment tab to pop open the data in the data viewer.
#code here
  1. Create a subset of the data frame for heterosexual relationships on screen. Save this into a new pipeline.
#code here
  1. Split the data for heterosexual relationships into three – where woman is older, where man is older, where they are the same age. Save these subsets as three appropriately named data frames. Confirm that these new objects appear in your Environment tab and that the sum of the number of observations in the two new data frames add to the number of observations in the original data frame.
Note

If you are stuck, here is an idea on a process to get started:

  • Use mutate to create a variable that tells which case the row has: older woman, older man, or same age

  • filter the data frame based on the previously created variable

# code here

Write the three new datasets you created as .csv files in the data folder:

# code here

s## Part 2: Sales

Sales data are stored in an Excel file that looks like the following:

Read in the Excel file called sales.xlsx from the data-raw/ folder such that it looks like the following.

Fill in the blanks in the following code to accomplish this.

sales_raw <- read_excel(
  file_name, 
  skip = ___,
  col_names = _____
  )
Tip

The skip and col_names attributes are very useful for reading in messy data!

  • skip tells R how many rows at the top of the file the function should ignore

  • col_names tells R what to name the columns it imports

Stretch goal: Manipulate the sales data such such that it looks like the following.

# code here

Why should we bother with writing code for reading the data in by skipping columns and assigning variable names as well as cleaning it up in multiple steps instead of opening the Excel file and editing the data in there to prepare it for a clean import?