library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
# 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)
)

Task 1 Inner Join (3 points) Perform an inner join between the customers and orders datasets.

Task1 <- inner_join(customers, orders, by = "customer_id")

#How many rows are in the result?

four

#Why are some customers or orders not included in the result?

Inner join only connects data that have matching keys in both data set

print(Task1)
## # A tibble: 4 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300

Task 2 Left Join (3 points) Perform a left join with customers as the left table and orders as the right table.

Task2 <- left_join(customers, orders, by = "customer_id")

#How many rows are in the result?

six

#Explain why this number differs from the inner join result. Display the result

Left join includes all the rows from customers but only matching rows from orders

print(Task2)
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           4 David   Houston           NA <NA>        NA
## 6           5 Eve     Phoenix           NA <NA>        NA

Task 3 Right Join (3 points) Perform a right join with customers as the left table and orders as the right table.

Task3 <- right_join(customers, orders, by = "customer_id")

#How many rows are in the result?

six

#Which customer_ids in the result have NULL for customer name and city? Explain why. Display the result

six and seven, There is no matching data for customer ids six and seven in the Customers data set.

print(Task3)
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           6 <NA>    <NA>             105 Camera     600
## 6           7 <NA>    <NA>             106 Printer    150

Task 4 Full Join (3 points) Perform a full join between customers and orders.

Task4 <- full_join(customers, orders, by = "customer_id")

#How many rows are in the result?

eight

#Identify any rows where there’s information from only one table. Explain these results. Display the result

Rows five through eight only have data from one of each set. Rows five and six Only have data from the customer set whereas rows seven and eight only have data from the orders data set. This is due to the full join including every row in each set but the two sets are missing corresponding data for these rows.

print(Task4)
## # A tibble: 8 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           4 David   Houston           NA <NA>        NA
## 6           5 Eve     Phoenix           NA <NA>        NA
## 7           6 <NA>    <NA>             105 Camera     600
## 8           7 <NA>    <NA>             106 Printer    150

Task 5 Semi Join (3 points) Perform a semi join with customers as the left table and orders as the right table.

Task5 <- semi_join(customers, orders, by = "customer_id")

#How many rows are in the result?

Three

#How does this result differ from the inner join result? Display the result

This differs from the inner join result due to the semi join only displaying the customers who made only one order.

print(Task5)
## # A tibble: 3 × 3
##   customer_id name    city       
##         <dbl> <chr>   <chr>      
## 1           1 Alice   New York   
## 2           2 Bob     Los Angeles
## 3           3 Charlie Chicago

Task 6 Anti Join (3 points) Perform an anti join with customers as the left table and orders as the right table.

Task6 <- anti_join(customers, orders, by = "customer_id")

#Which customers are in the result?

David and Eve

#Explain what this result tells you about these customers. Display the result

These are the customers who have not placed any orders.

print(Task6)
## # A tibble: 2 × 3
##   customer_id name  city   
##         <dbl> <chr> <chr>  
## 1           4 David Houston
## 2           5 Eve   Phoenix

Task 7 Practical Application (4 points) Imagine you’re analyzing customer behavior.

#Which join would you use to find all customers, including those who haven’t placed any orders? Why?

Full Join

#Which join would you use to find only the customers who have placed orders? Why?

Right Join

#Write the R code for both scenarios. Display the result

Task7 <- full_join(customers, orders, by = "customer_id")
print(Task7)
## # A tibble: 8 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           4 David   Houston           NA <NA>        NA
## 6           5 Eve     Phoenix           NA <NA>        NA
## 7           6 <NA>    <NA>             105 Camera     600
## 8           7 <NA>    <NA>             106 Printer    150
Task7.2 <- right_join(customers, orders, by = "customer_id")
print(Task7.2)
## # A tibble: 6 × 6
##   customer_id name    city        order_id product amount
##         <dbl> <chr>   <chr>          <dbl> <chr>    <dbl>
## 1           1 Alice   New York         101 Laptop    1200
## 2           2 Bob     Los Angeles      102 Phone      800
## 3           2 Bob     Los Angeles      104 Desktop   1500
## 4           3 Charlie Chicago          103 Tablet     300
## 5           6 <NA>    <NA>             105 Camera     600
## 6           7 <NA>    <NA>             106 Printer    150

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.

summary_result <- customers %>%
  full_join(orders, by = "customer_id") %>%
  group_by(name, city) %>%
  summarize(
    total_orders = n(),  # Count of orders
    total_amount_spent = sum(amount, na.rm = TRUE)  # Total amount spent
  ) %>%
  ungroup()  # Remove grouping
## `summarise()` has grouped output by 'name'. You can override using the
## `.groups` argument.

Display the summary result

print(summary_result)
## # A tibble: 6 × 4
##   name    city        total_orders total_amount_spent
##   <chr>   <chr>              <int>              <dbl>
## 1 Alice   New York               1               1200
## 2 Bob     Los Angeles            2               2300
## 3 Charlie Chicago                1                300
## 4 David   Houston                1                  0
## 5 Eve     Phoenix                1                  0
## 6 <NA>    <NA>                   2                750