Assignment: Understanding and Applying Joins in R with dplyr

Objective

This assignment aims to assess your understanding of different types of joins in R using the dplyr package. You will work with sample datasets, perform various joins, and answer questions based on the results.

Setup

First, ensure you have the dplyr package installed and loaded:

library(dplyr)

Datasets

Use the following code to create two datasets:

# Dataset 1: Customers
customers <- tibble(
  customer_id = c(1, 2, 3, 4, 5),
  name = c("Alice", "Bob", "Charlie", "David", "Eve"),
  city = c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix")
)

# Dataset 2: Orders
orders <- tibble(
  order_id = c(101, 102, 103, 104, 105, 106),
  customer_id = c(1, 2, 3, 2, 6, 7),
  product = c("Laptop", "Phone", "Tablet", "Desktop", "Camera", "Printer"),
  amount = c(1200, 800, 300, 1500, 600, 150)
)

Tasks

  1. Inner Join (3 points) Perform an inner join between the customers and orders datasets.
    1. How many rows are in the result?
    2. Why are some customers or orders not included in the result?
    3. Display the result
  2. Left Join (3 points) Perform a left join with customers as the left table and orders as the right table.
    1. How many rows are in the result?
    2. Explain why this number differs from the inner join result.
    3. Display the result
  3. Right Join (3 points) Perform a right join with customers as the left table and orders as the right table.
    1. How many rows are in the result?
    2. Which customer_ids in the result have NULL for customer name and city? Explain why.
    3. Display the result
  4. Full Join (3 points) Perform a full join between customers and orders.
    1. How many rows are in the result?
    2. Identify any rows where there’s information from only one table. Explain these results.
    3. Display the result
  5. Semi Join (3 points) Perform a semi join with customers as the left table and orders as the right table.
    1. How many rows are in the result?
    2. How does this result differ from the inner join result?
    3. Display the result
  6. Anti Join (3 points) Perform an anti join with customers as the left table and orders as the right table.
    1. Which customers are in the result?
    2. Explain what this result tells you about these customers.
    3. Display the result
  7. Practical Application (4 points) Imagine you’re analyzing customer behavior.
    1. Which join would you use to find all customers, including those who haven’t placed any orders? Why?
    2. Which join would you use to find only the customers who have placed orders? Why?
    3. Write the R code for both scenarios.
    4. Display the result
  8. Challenge Question (3 points) Create a summary that shows each customer’s name, city, total number of orders, and total amount spent. Include all customers, even those without orders. Hint: You’ll need to use a combination of joins and group_by/summarize operations.

Submission

Submit the assignment as a link to RPubs. (3 points)

Total Points: 25

Good luck!