AE 09: Importing Data
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
- Load the data from
age-gaps.csvin yourdataand assign it toage_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- Create a subset of the data frame for heterosexual relationships on screen. Save this into a new pipeline.
#code here- 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.
If you are stuck, here is an idea on a process to get started:
Use
mutateto create a variable that tells which case the row has: older woman, older man, or same agefilterthe data frame based on the previously created variable
# code hereWrite the three new datasets you created as .csv files in the data folder:
# code heres## 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 = _____
)The skip and col_names attributes are very useful for reading in messy data!
skiptells R how many rows at the top of the file the function should ignorecol_namestells R what to name the columns it imports
Stretch goal: Manipulate the sales data such such that it looks like the following.
# code hereWhy 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?