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) ) # Inner Join inner_join <- inner_join(customers, orders, by = “customer_id”) print(inner_join_result) # Left Join left_join <- left_join(customers, orders, by = “customer_id”) print(left_join_result) # Right Join right_join <- right_join(customers, orders, by = “customer_id”) print(right_join_result) # Full Join full_join <- full_join(customers, orders, by = “customer_id”) print(full_join_result) # Semi Join semi_join <- semi_join(customers, orders, by = “customer_id”) print(semi_join_result) # Anti Join anti_join <- anti_join(customers, orders, by = “customer_id”) print(anti_join_result) # Customers including those without orders all_customers <- left_join(customers, orders, by = “customer_id”) print(all_customers_result) # Customers who have placed orders customers_with_orders <- inner_join(customers, orders, by = “customer_id”) print(customers_with_orders_result) # Full join to include all customers and orders, even those without orders customer_orders <- customers %>% full_join(orders, by = “customer_id”) %>% group_by(customer_id, name, city) %>% summarize( total_orders = n(), total_amount = sum(amount, na.rm = TRUE) ) %>% arrange(customer_id)
print(customer_orders_summary)