library(dplyr) library(stats) library(base)
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_result <- dplyr::inner_join(customers, orders, by = “customer_id”) print(“Inner Join Result:”) print(inner_join_result)
left_join_result <- left_join(customers, orders, by = “customer_id”) print(“Left Join Result:”) print(left_join_result)
right_join_result <- right_join(customers, orders, by = “customer_id”) print(“Right Join Result:”) print(right_join_result)
full_join_result <- full_join(customers, orders, by = “customer_id”) print(“Full Join Result:”) print(full_join_result)
semi_join_result <- semi_join(customers, orders, by = “customer_id”) print(“Semi Join Result:”) print(semi_join_result)
anti_join_result <- anti_join(customers, orders, by = “customer_id”) print(“Anti Join Result:”) print(anti_join_result)
all_customers_result <- left_join(customers, orders, by = “customer_id”) print(“All Customers (including those without orders):”) print(all_customers_result)
customers_with_orders_result <- inner_join(customers, orders, by = “customer_id”) print(“Customers with Orders:”) print(customers_with_orders_result)
customer_summary <- customers %>% left_join(orders, by = “customer_id”) %>% group_by(customer_id, name, city) %>% summarize( total_orders = sum(!is.na(order_id)), # Count non-NA order_id as total orders total_amount_spent = sum(amount, na.rm = TRUE) # Sum the amount spent )
print(“Customer Summary (total orders and amount spent):”) print(customer_summary)