library(dplyr) # 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 inner_join_result <- customers %>% inner_join(orders, by = “customer_id”)

Display the result

print(inner_join_result)

Check number of rows

nrow(inner_join_result)

Left Join

left_join_result <- customers %>% left_join(orders, by = “customer_id”)

Display the result

print(left_join_result)

Check number of rows

nrow(left_join_result)

Right Join

right_join_result <- customers %>% right_join(orders, by = “customer_id”)

Display the result

print(right_join_result)

Check number of rows

nrow(right_join_result)

Full Join

full_join_result <- customers %>% full_join(orders, by = “customer_id”)

Display the result

print(full_join_result)

Check number of rows

nrow(full_join_result)

Semi Join

semi_join_result <- customers %>% semi_join(orders, by = “customer_id”)

Display the result

print(semi_join_result)

Check number of rows

nrow(semi_join_result)

Anti Join

anti_join_result <- customers %>% anti_join(orders, by = “customer_id”)

Display the result

print(anti_join_result)

All customers, including those who haven’t placed orders

all_customers <- customers %>% left_join(orders, by = “customer_id”)

Display the result

print(all_customers)

Only customers who have placed orders

customers_with_orders <- customers %>% inner_join(orders, by = “customer_id”)

Display the result

print(customers_with_orders)

Summary of customer orders and total amount spent

customer_summary <- customers %>% left_join(orders, by = “customer_id”) %>% group_by(customer_id, name, city) %>% summarize( total_orders = n(), total_amount_spent = sum(amount, na.rm = TRUE) )

Display the result

print(customer_summary)