I have a dataframe looking like this:
Model Month Country Price
1 Audi TT 2016-03 NL 187
1 Audi TT 2017-03 NL 220
1 Audi TT 2016-03 DE 190
4 Volkswagen Golf 2016-08 NL 204
Now, I want to aggregate the data over the countries such that I only have one observation for each model at a specific date. To do this, I use:
DT = data.table(test_data)
test_data_total = DT[, lapply(.SD, mean), by = Month]
from Can dplyr summarise over several variables without listing each one?.
However, this only works when dealing with numeric variables. In my case the Model
variable is a character so this gives me an error, while I want to print only the model name once then. So afterwards it should look like this:
Model Month Country Price
1 Audi TT 2016-03 NL avg
1 Audi TT 2017-03 NL 220
4 Volkswagen Golf 2016-08 NL 204
Does someone know how to do this?