Lab 0: Getting Started with dplyr

Your First Data Analysis

Author

Isabelle Li

Published

September 22, 2025

Overview

Welcome to your first lab! In this (not graded) assignment, you’ll practice the fundamental dplyr operations I overviewed in class using car sales data. This lab will help you get comfortable with:

  • Basic data exploration
  • Column selection and manipulation
  • Creating new variables
  • Filtering data
  • Grouping and summarizing

Instructions: Copy this template into your portfolio repository under a lab_0/ folder, then complete each section with your code and answers. You will write the code under the comment section in each chunk. Be sure to also copy the data folder into your lab_0 folder.

Setup

# Load the tidyverse library
library(tidyverse)

# Read in the car sales data
# Make sure the data file is in your lab_0/data/ folderins
car_data <- read_csv("data/car_sales_data.csv")

Exercise 1: Getting to Know Your Data

1.1 Data Structure Exploration

Explore the structure of your data and answer these questions:

# Use glimpse() to see the data structure
glimpse(car_data)
Rows: 50,000
Columns: 7
$ Manufacturer          <chr> "Ford", "Porsche", "Ford", "Toyota", "VW", "Ford…
$ Model                 <chr> "Fiesta", "718 Cayman", "Mondeo", "RAV4", "Polo"…
$ `Engine size`         <dbl> 1.0, 4.0, 1.6, 1.8, 1.0, 1.4, 1.8, 1.4, 1.2, 2.0…
$ `Fuel type`           <chr> "Petrol", "Petrol", "Diesel", "Hybrid", "Petrol"…
$ `Year of manufacture` <dbl> 2002, 2016, 2014, 1988, 2006, 2018, 2010, 2015, …
$ Mileage               <dbl> 127300, 57850, 39190, 210814, 127869, 33603, 866…
$ Price                 <dbl> 3074, 49704, 24072, 1705, 4101, 29204, 14350, 30…
# Check the column names
names(car_data)
[1] "Manufacturer"        "Model"               "Engine size"        
[4] "Fuel type"           "Year of manufacture" "Mileage"            
[7] "Price"              
# Look at the first few rows
head(car_data)
# A tibble: 6 × 7
  Manufacturer Model     `Engine size` `Fuel type` `Year of manufacture` Mileage
  <chr>        <chr>             <dbl> <chr>                       <dbl>   <dbl>
1 Ford         Fiesta              1   Petrol                       2002  127300
2 Porsche      718 Caym…           4   Petrol                       2016   57850
3 Ford         Mondeo              1.6 Diesel                       2014   39190
4 Toyota       RAV4                1.8 Hybrid                       1988  210814
5 VW           Polo                1   Petrol                       2006  127869
6 Ford         Focus               1.4 Petrol                       2018   33603
# ℹ 1 more variable: Price <dbl>

Questions to answer: - How many rows and columns does the dataset have? - What types of variables do you see (numeric, character, etc.)? - Are there any column names that might cause problems? Why?

Your answers: - Rows: 50,000 - Columns: 7 - Variable types: Character,Double - Problematic names: Name with spaces in between

1.2 Tibble vs Data Frame

Compare how tibbles and data frames display:

# Look at the tibble version (what we have)
car_data
# A tibble: 50,000 × 7
   Manufacturer Model    `Engine size` `Fuel type` `Year of manufacture` Mileage
   <chr>        <chr>            <dbl> <chr>                       <dbl>   <dbl>
 1 Ford         Fiesta             1   Petrol                       2002  127300
 2 Porsche      718 Cay…           4   Petrol                       2016   57850
 3 Ford         Mondeo             1.6 Diesel                       2014   39190
 4 Toyota       RAV4               1.8 Hybrid                       1988  210814
 5 VW           Polo               1   Petrol                       2006  127869
 6 Ford         Focus              1.4 Petrol                       2018   33603
 7 Ford         Mondeo             1.8 Diesel                       2010   86686
 8 Toyota       Prius              1.4 Hybrid                       2015   30663
 9 VW           Polo               1.2 Petrol                       2012   73470
10 Ford         Focus              2   Diesel                       1992  262514
# ℹ 49,990 more rows
# ℹ 1 more variable: Price <dbl>
# Convert to regular data frame and display
car_df <- as.data.frame(car_data)

Question: What differences do you notice in how they print?

Your answer: - Show first 10 rows by default - Display column names - fits nicely in the screen

Exercise 2: Basic Column Operations

2.1 Selecting Columns

Practice selecting different combinations of columns:

# Select just Model and Mileage columns
select(car_data,"Model","Mileage")
# A tibble: 50,000 × 2
   Model      Mileage
   <chr>        <dbl>
 1 Fiesta      127300
 2 718 Cayman   57850
 3 Mondeo       39190
 4 RAV4        210814
 5 Polo        127869
 6 Focus        33603
 7 Mondeo       86686
 8 Prius        30663
 9 Polo         73470
10 Focus       262514
# ℹ 49,990 more rows
# Select Manufacturer, Price, and Fuel type
select(car_data,"Manufacturer","Price","Fuel type")
# A tibble: 50,000 × 3
   Manufacturer Price `Fuel type`
   <chr>        <dbl> <chr>      
 1 Ford          3074 Petrol     
 2 Porsche      49704 Petrol     
 3 Ford         24072 Diesel     
 4 Toyota        1705 Hybrid     
 5 VW            4101 Petrol     
 6 Ford         29204 Petrol     
 7 Ford         14350 Diesel     
 8 Toyota       30297 Hybrid     
 9 VW            9977 Petrol     
10 Ford          1049 Diesel     
# ℹ 49,990 more rows
# Challenge: Select all columns EXCEPT Engine Size
select(car_data,!"Engine size")
# A tibble: 50,000 × 6
   Manufacturer Model      `Fuel type` `Year of manufacture` Mileage Price
   <chr>        <chr>      <chr>                       <dbl>   <dbl> <dbl>
 1 Ford         Fiesta     Petrol                       2002  127300  3074
 2 Porsche      718 Cayman Petrol                       2016   57850 49704
 3 Ford         Mondeo     Diesel                       2014   39190 24072
 4 Toyota       RAV4       Hybrid                       1988  210814  1705
 5 VW           Polo       Petrol                       2006  127869  4101
 6 Ford         Focus      Petrol                       2018   33603 29204
 7 Ford         Mondeo     Diesel                       2010   86686 14350
 8 Toyota       Prius      Hybrid                       2015   30663 30297
 9 VW           Polo       Petrol                       2012   73470  9977
10 Ford         Focus      Diesel                       1992  262514  1049
# ℹ 49,990 more rows

2.2 Renaming Columns

Let’s fix a problematic column name:

# Rename 'Year of manufacture' to year
car_data <- car_data %>%
  rename(year='Year of manufacture')

# Check that it worked
names(car_data)
[1] "Manufacturer" "Model"        "Engine size"  "Fuel type"    "year"        
[6] "Mileage"      "Price"       

Question: Why did we need backticks around Year of manufacture but not around year?

Your answer: There’s no space in between

Exercise 3: Creating New Columns

3.1 Calculate Car Age

# Create an 'age' column (2025 minus year of manufacture)

# Create a mileage_per_year column  


# Look at your new columns
#select(car_data, Model, year, age, Mileage, mileage_per_year)

3.2 Categorize Cars

# Create a price_category column where if price is < 15000, its is coded as budget, between 15000 and 30000 is midrange and greater than 30000 is mid-range (use case_when)


# Check your categories select the new column and show it

Exercise 4: Filtering Practice

4.1 Basic Filtering

# Find all Toyota cars


# Find cars with mileage less than 30,000


# Find luxury cars (from price category) with low mileage

4.2 Multiple Conditions

# Find cars that are EITHER Honda OR Nissan


# Find cars with price between $20,000 and $35,000


# Find diesel cars less than 10 years old

Question: How many diesel cars are less than 10 years old?

Your answer: [YOUR ANSWER]

Exercise 5: Grouping and Summarizing

5.1 Basic Summaries

# Calculate average price by manufacturer
avg_price_by_brand <- car_data %>%
  group_by(Manufacturer) %>%
  summarize(avg_price = mean(Price, na.rm = TRUE))

avg_price_by_brand
# A tibble: 5 × 2
  Manufacturer avg_price
  <chr>            <dbl>
1 BMW             24429.
2 Ford            10672.
3 Porsche         29104.
4 Toyota          14340.
5 VW              10363.
# Calculate average mileage by fuel type


# Count cars by manufacturer

5.2 Categorical Summaries

# Frequency table for price categories

Submission Notes

To submit this lab: 1. Make sure your code runs without errors 2. Fill in all the “[YOUR ANSWER]” sections and complete all of the empty code! 3. Save this file in your portfolio’s lab_0/ folder 4. Commit and push to GitHub 5. Check that it appears on your GitHub Pages portfolio site

Questions? Post on the canvas discussion board or come to office hours!