Compute monthly totals, seasonal indices and a trend slope from daily sales in base R
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Implement base R functions in `/app/solution.R` that expose seasonality and trend in daily sales. Each takes a `data.frame` with a `date` (`Date`) column and a numeric `sales` column.
- `aggregate_monthly(df)`: columns `year` (integer), `month` (integer 1-12) and `total` (sum of sales), sorted by year then month, with rownames `1..n`.
- `seasonal_indices(df)`: columns `month` (one row per calendar month present, ascending) and `index`, where `index` is the mean of the monthly totals for that month divided by the mean of all monthly totals.
- `trend_slope(df)`: fit `lm(total ~ t)` on the chronological monthly totals with `t = 1, 2, ..., n` and return the slope as a numeric scalar.
- `deseasonalize(df)`: the `aggregate_monthly` result (same row order) plus a column `adjusted` equal to `total` divided by that month's index from `seasonal_indices`.
Example:
```r
df <- data.frame(
date = as.Date(c("2023-01-05","2023-01-20","2023-02-10","2024-01-15")),
sales = c(100, 50, 80, 200)
)
aggregate_monthly(df)
# year month total
# 1 2023 1 150
# 2 2023 2 80
# 3 2024 1 200
trend_slope(df) # 25, a single numeric scalar
```
```hint Reuse one series
Three of the four functions need the same monthly table. Decide which function produces it and build the others on top, so every result is computed from identical totals.
```
```hint Which rows feed the mean
Check which rows go into the denominator of the index. The spec averages every year-month total in the aggregated table, so look at what that means for a month that appears in two years.
```
### Constraints & Assumptions
- Base R only.
- Input is non-empty and well-formed. No plotting or persistence is needed.
### What a Strong Answer Covers
- Monthly aggregation with integer `year` and `month`, sorted by year then month, with row names reset
- The seasonal index as a month's mean total over the grand mean of all monthly totals, one row per month present
- A trend slope from `lm` on a chronological counter, returned as a plain numeric scalar
- Deseasonalized values matched to the correct month's index without reordering rows
- Awareness of how gaps in the monthly series and incomplete years affect the slope and the indices
### Follow-up Questions
- How would you handle months with zero sales or missing months in the series?
- Why can a linear slope be misleading, and what alternative would you use?
- When some months are observed in more years than others, these indices do not average to 1. Does that matter, and how would you normalize them?
Overview: Write base R functions that turn daily sales into monthly totals, compute a seasonal index for each calendar month, fit a linear trend slope with lm, and deseasonalize the monthly series. It tests aggregation with dates, grouped means, simple regression and how gaps in a time series affect the results.