implementing `data.table`'s `dcast()` when using multiple aggregation functions in R
I'm working on a project and hit a roadblock. I'm having trouble with the `dcast()` function from the `data.table` package in R, specifically when I try to apply multiple aggregation functions to the same variable. My dataset has daily sales data across different categories, and I want to aggregate the total sales and average sales per category. Here is the simplified version of my data: ```r library(data.table) # Sample data set.seed(123) data <- data.table( date = rep(as.Date('2023-01-01') + 0:4, each = 2), category = rep(c('A', 'B'), 5), sales = sample(100:200, 10) ) print(data) ``` When I try to use `dcast()` like this: ```r result <- dcast(data, date ~ category, value.var = 'sales', fun.aggregate = list(total = sum, avg = mean)) ``` I get the following behavior message: ``` behavior in dcast.data.table(data, date ~ category, value.var = "sales", : want to use multiple aggregation functions; use the `fun.aggregate` argument instead. ``` I've looked through the documentation and tried to pass `fun.aggregate` as a single function, but that doesn't meet my requirement for both total and average. I've also explored using `melt()` prior to `dcast()`, but the performance hit with larger datasets is concerning. I want to keep my operations efficient, especially with larger datasets that could reach 1 million rows. Is there a recommended way to achieve this using `data.table`, or should I consider a different approach? Any tips on performance optimization would also be appreciated. This is part of a larger web app I'm building. What am I doing wrong? Any ideas what could be causing this?