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
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")
)
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 <- customers %>%
inner_join(orders, by = "customer_id")
####How many rows are in the result?
Q1_rows <- nrow(Q1)
print(paste("Number of rows in the result:", Q1_rows))
## [1] "Number of rows in the result: 4"
####Why are some customers or orders not included in the result?
#In an inner join, both datasets must have matching values in the key column for rows to appear in the final result. If there is no match in either dataset, that row is omitted.
####Display the result
print(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 <- customers %>%
left_join(orders, by = "customer_id")
Q2_rows <- nrow(Q2)
print(paste("Number of rows in the result:", Q2_rows))
## [1] "Number of rows in the result: 6"
Left join preserves all rows from the left table, which is why it has more rows than the inner join, where only rows with matching customer id’s are retained
print(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 <- customers %>%
right_join(orders, by = "customer_id")
Q3_rows <- nrow(Q3)
print(paste("Number of rows in the result:", Q3_rows))
## [1] "Number of rows in the result: 6"
Customer_id 6 and 7 are not found in the customers dataset, so the name and city columns for these customer_ids will be NULL.
print(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 <- customers %>%
full_join(orders, by = "customer_id")
Q4_rows <- nrow(Q4)
print(paste("Number of rows in the result:", Q4_rows))
## [1] "Number of rows in the result: 8"
Rows where customer_id is present in customers but not in orders will have NA for order information Rows where customer_id is present in orders but not in customers will have NA for name and city
print(Q4)
## # 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
Q5 <- customers %>%
semi_join(orders, by = "customer_id")
Q5_rows <- nrow(Q5)
print(paste("Number of rows in the result:", Q5_rows))
## [1] "Number of rows in the result: 3"
In a semi join, only the rows from the left table (customers) are returned, No columns from the right table (orders) are included. It shows the customers who have orders, not order details
print(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 <- customers %>%
anti_join(orders, by = "customer_id")
Customers in the customer table but not in the order table
Customers in the result have not placed any orders
print(Q6)
## # A tibble: 2 × 3
## customer_id name city
## <dbl> <chr> <chr>
## 1 4 David Houston
## 2 5 Eve Phoenix
Left join since it keeps all rows from the customers table, regardless of status on the order table
customers_with_orders <- customers %>%
left_join(orders, by = "customer_id")
print (customers_with_orders)
## # 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
Inner join because it only displays rows that have a match in both tables, meaning it will only include customers who were also included in the orders table
customers_with_orders <- customers %>%
inner_join(orders, by = "customer_id")
print(customers_with_orders)
## # 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
customer_orders <- customers %>%
left_join(orders, by = "customer_id")
summary_result <- customer_orders %>%
group_by(name, city) %>%
summarize(
total_orders = n_distinct(order_id),
total_amount_spent = sum(amount, na.rm = TRUE)
)
## `summarise()` has grouped output by 'name'. You can override using the
## `.groups` argument.