Average order value per customer in R with a left join
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Write an R function `avg_order_value(customers, orders)` that left-joins `orders` onto `customers` by `customer_id` and computes each customer's order count and mean order amount. Every customer must appear in the result, including customers with no orders.
The result must meet this contract:
- It is a plain `data.frame` (not a tibble) with columns in this order: `customer_id`, `name`, `n_orders`, `avg_order_value`.
- `customer_id` keeps its input type and `name` is character.
- `n_orders` is an integer count, `0` for a customer with no orders.
- `avg_order_value` is the numeric mean of `amount`, and `NA_real_` for a customer with no orders.
- There is one row per customer, sorted ascending by `customer_id`, with default row names (`1..n`).
Example:
```r
customers <- data.frame(customer_id = c(1, 2, 3), name = c("Ann", "Bob", "Cat"))
orders <- data.frame(order_id = 1:3, customer_id = c(1, 1, 2), amount = c(10, 30, 50))
avg_order_value(customers, orders)
# customer_id name n_orders avg_order_value
# 1 1 Ann 2 20
# 2 2 Bob 1 50
# 3 3 Cat 0 NA
```
```hint Count carefully
Picture the row a left join produces for Cat, who has no orders. What does `n()` return for her group, and what does `mean(amount, na.rm = TRUE)` return?
```
```hint Check the types
Look at the type `n()` returns, the type of the value you fill in for missing counts, and the class of the object a dplyr pipeline returns.
```
### Constraints & Assumptions
- `customers` has one row per `customer_id`, with columns `customer_id` and `name`.
- `orders` has columns `order_id`, `customer_id` and `amount`, with any number of orders per customer, including none.
- An order whose `customer_id` does not appear in `customers` has no customer row to attach to and does not appear in the output.
- You may use dplyr or base R.
### What a Strong Answer Covers
- Customers with no orders survive the join with a count of `0` and an average of `NA`, never a count of 1, an average of 0, or `NaN`.
- The output types match the contract: integer `n_orders`, double `avg_order_value`, character `name`, and `customer_id` unchanged.
- The function returns a plain data frame with the required column order, explicit sorting and default row names.
- The answer says how missing `amount` values and orders for unknown customers are treated.
### Follow-up Questions
- How would you handle `NA` values in `amount`?
- Why sort explicitly instead of relying on the order the join returns?
- How would you also report each customer's largest order, and what should that column hold for customers with no orders?
Overview: Write an R function that left-joins orders onto customers and returns each customer's order count and average order value as a plain data frame. It tests handling of customers with no orders, integer and NA column types, and deterministic sorting of the output.