q1 <- inner_join(customers , orders)
## Joining with `by = join_by(customer_id)`
a) How many rows are in the result?
nrow(q1)
## [1] 4
b) Why are some customers or orders not included in the result?
inner join returns when there is a match on both tables The customer data has only 3 customers in the order table where one customer has 2 orders
c) 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
q2 <- left_join(customers, orders, by = "customer_id")
a) How many rows are in the result?
nrow(q2)
## [1] 6
b) Explain why this number differs from the inner join result.
In an inner join, only the records that have matching keys (customer_id in this case) in both tables are included. In the left join, all records from the left table (customers) are included, even if there are no matches in the right table (orders). This is why customers like David and Eve, who don’t have orders, are still present in the left join result.
c) 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")
a) How many rows are in the result?
nrow(q3)
## [1] 6
b) Which customer_ids in the result have NULL for customer name and city? Explain why.
The customer_ids 6 and 7 have NULL for name and city because there are no matching records for these customer_ids in the customers table.
c) 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")
a) How many rows are in the result?
nrow(q4)
## [1] 8
b) Identify any rows where there’s information from only one table. Explain these results.
Rows with NA in order_id correspond to customers who have no orders (David with customer_id 4 and Eve with customer_id 5). Rows with NA in name correspond to orders with no matching customer (orders with customer_id 6 and 7).
c) 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")
a) How many rows are in the result?
nrow(q5)
## [1] 3
b) How does this result differ from the inner join result?
A semi join only returns rows from the customers table (the left table) and does not include any columns from the orders table. An inner join returns matching rows from both tables, so it includes columns from both customers and orders.
c) 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")
a) Which customers are in the result?
The customers in the result are those with customer_ids 4 and 5 (David and Eve). These are the customers in the customers table who have no matching customer_id in the orders table.
b) Explain what this result tells you about these customers.
This result indicates that David and Eve have not placed any orders. They exist in the customers table but do not have a corresponding entry in the orders table, which means they haven’t made any purchases.
c) Display the result
head(q6)
## # A tibble: 2 × 3
## customer_id name city
## <dbl> <chr> <chr>
## 1 4 David Houston
## 2 5 Eve Phoenix
a) Which join would you use to find all customers, including those who haven’t placed any orders? Why?
For this scenario, a left join is suitable. A left join will return all customers, including those who haven’t placed any orders, by including all records from the customers table and matching records from the orders table where possible. This way, customers without orders will appear with NA values in the order-related columns.
b) Which join would you use to find only the customers who have placed orders? Why?
For this, an inner join is appropriate. An inner join will return only those customers who have corresponding entries in the orders table, thus showing only customers who have placed orders.
c) Write the R code for both scenarios.
q7a <- left_join(customers, orders, by = "customer_id")
q7b <- inner_join(customers, orders, by = "customer_id")
d) Display the result
All Customers (including those without orders)
head(q7a)
## # 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
Customers who have placed orders:
head(q7b)
## # 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
q8 <- q2 %>%
group_by(customer_id, name, city) %>%
summarize(
total_orders = n_distinct(order_id, na.rm = TRUE),
total_amount_spent = sum(amount, na.rm = TRUE)
)
## `summarise()` has grouped output by 'customer_id', 'name'. You can override
## using the `.groups` argument.
head(q8)
## # A tibble: 5 × 5
## # Groups: customer_id, name [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