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)
)
q1 <- inner_join(customers , orders)
## Joining with `by = join_by(customer_id)`
How many rows are in the result?
There are 4 rows
Why are some customers or orders not included in the
result?
An inner join returns where there is a match in both tables. The
customer data has only 3 customers in the orderwhere one customer has 2
orders.
Display the result
head(q1)
## # 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
# Perform a left join
q2 <- left_join(customers, orders, by = "customer_id")
How many rows are in the result?
There are 6 rows
Explain why this number differs from the inner join
result.
The number of rows in a left join is higher than in an inner join
because the left join keeps all rows from the left table (in this case,
the customers table), even if there’s no matching data in the orders
table.
Display the result
head(q2)
## # 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
q3 <- right_join(customers, orders, by = "customer_id")
How many rows are in the result?
There are 6 rows
Which customer_ids in the result have NULL for customer
name and city? Explain why.
Customer_id 6 and customer_id 7 have NULL for customer name and city.
These are the customer_ids that appear in orders but do not exist in the
customers table, which is why their name and city are NULL. They
represent orders placed by customers who are not listed in the customers
table.
Display the result
head(q3)
## # 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
q4 <- full_join(customers, orders, by = "customer_id")
How many rows are in the result?
There are 8 rows
Identify any rows where there’s information from only one
table. Explain these results.
Rows 5, 6, 7, and 8 have information from only one table. These rows
occur when a customer_id exists in one table but not the other.
Display the result
head(q4)
## # 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
q5 <- semi_join(customers, orders, by = "customer_id")
How many rows are in the result?
There are 3 rows
How does this result differ from the inner join
result?
The semi join includes only the columns from the customers table for
matching customer_ids. In contrast, the inner join includes columns from
both tables for matching rows. The row count will be the same as the
inner join, but the semi join doesn’t include information about the
orders (like order_id, product, or amount).
Display the result
head(q5)
## # 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
q6 <- 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.
The result tells us that these customers are registered in the customers
table but have not made any purchases recorded in the orders table.
Essentially, these customers haven’t placed any orders.
Display the result
head(q6)
## # A tibble: 2 × 3
## customer_id name city
## <dbl> <chr> <chr>
## 1 4 David Houston
## 2 5 Eve Phoenix
q7 <- left_join(customers, orders, by = "customer_id")
Which join would you use to find all customers, including
those who haven’t placed any orders? Why?
You would use a left join for this scenario. A left join will return all
customers from the customers table, along with any matching orders from
the orders table. If a customer hasn’t placed any orders, their order
details will be NA, but they will still be included in the
result.
Which join would you use to find only the customers who
have placed orders? Why?
You would use a semi join for this scenario. A semi join returns only
the customers who have placed orders, providing a clean list of those
customers without duplicating order details. This is efficient when you
only need customer information without any order data.
Write the R code for both scenarios.
all_customers_with_orders <- left_join(customers, orders, by = "customer_id")
customers_with_orders <- semi_join(customers, orders, by = "customer_id")
head(q7)
## # 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
customer_orders_summary <- left_join(customers, orders, by = "customer_id")
summary_result <- customer_orders_summary %>%
group_by(customer_id, name, city) %>%
summarize(
total_orders = sum(!is.na(order_id)), # Count only non-NA order_id
total_amount_spent = sum(amount, na.rm = TRUE) # Sum total amount spent
) %>%
ungroup()
## `summarise()` has grouped output by 'customer_id', 'name'. You can override
## using the `.groups` argument.
head(summary_result)
## # A tibble: 5 × 5
## customer_id name city total_orders total_amount_spent
## <dbl> <chr> <chr> <int> <dbl>
## 1 1 Alice New York 1 1200
## 2 2 Bob Los Angeles 2 2300
## 3 3 Charlie Chicago 1 300
## 4 4 David Houston 0 0
## 5 5 Eve Phoenix 0 0