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)
)
inner_join_result <- inner_join(customers, orders, by = "customer_id")
How many rows are in the result?
nrow(inner_join_result)
## [1] 4
Why are some customers or orders not included in the
result?
Inner join only displays rows where the customer_id matches and is
present in both databases.
Display the result
print(inner_join_result)
## # 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
left_join_result <- left_join(customers, orders, by = "customer_id")
How many rows are in the result?
nrow(left_join_result)
## [1] 6
Explain why this number differs from the inner join
result.
This number is different because it includes all the customers from the
customer database rather than just the matching customers from both
databases.
Display the result
print(left_join_result)
## # 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
right_join_result <- right_join(customers, orders, by = "customer_id")
How many rows are in the result?
nrow(right_join_result)
## [1] 6
Which customer_ids in the result have NULL for customer name
and city? Explain why.
The customer_ids 6 and 7 have null as customer name and city, this is
because it includes all the information from the order database rather
than just the matching order data from both databases.
Display the result
print(right_join_result)
## # 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
full_join_result <- full_join(customers, orders, by = "customer_id")
How many rows are in the result?
nrow(full_join_result)
## [1] 8
Identify any rows where there’s information from only one
table. Explain these results.
Rows 5 and 6 only have information from only one table, the customer
table, you can tell this by the NA displayed under the order_id,
product, and amount but the display of the name and city from the
customer table. Rows 7 and 8 only have information from one table, the
order table, you can tell this by the information showing NA for the
name and city information but having the order data.
Display the result
print(full_join_result)
## # 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
semi_join_result <- semi_join(customers, orders, by = "customer_id")
How many rows are in the result?
nrow(semi_join_result)
## [1] 3
How does this result differ from the inner join
result?
Semi join only returns all matching rows in the left table where there
is a match with the right, it yields one value per match. Inner join
returns all matched rows in both tables and can have multiple values per
customer_id.
Display the result
print(semi_join_result)
## # 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
anti_join_result <- anti_join(customers, orders, by = "customer_id")
Which customers are in the result?
Customer_id 4 and 5, their names are David and Eve and they are the only
two customers in the result.
Explain what this result tells you about these
customers.
This tells us that these customers in the left tables do not have
matches in the right table so they appear in the anti join.
Display the result
print( anti_join_result)
## # A tibble: 2 × 3
## customer_id name city
## <dbl> <chr> <chr>
## 1 4 David Houston
## 2 5 Eve Phoenix
Which join would you use to find all customers, including those who haven’t placed any orders? Why?
You would use a left join to find all customers including those who haven’t placed order because this function joins all the customer information from the left customer table and then corresponding information from the order table, it will display NA if they have not placed an order.
Which join would you use to find only the customers who have placed orders? Why?
You would use an inner join to find only customers who have placed orders because this function joins both tables only where the customer_id matches in both tables, this ensure each customer has at least one match.
Write the R code for both scenarios
left_join_result <- left_join(customers, orders, by = "customer_id")
inner_join_result <- inner_join(customers, orders, by = "customer_id")
Display the result
print(left_join_result)
## # 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
print(inner_join_result)
## # 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
summary_result <- customers %>%
left_join(orders, by = "customer_id") %>%
group_by(name, city) %>%
summarize(
total_orders = n(),
total_amount_spent = sum(amount, na.rm = TRUE),
.groups = "drop"
)
Display the result
print(summary_result)
## # A tibble: 5 × 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