Group by using base R
Dataset
I have a datasets with column year,quarter,Channel,sales,units
df <- structure(list(year = c(2013L, 2013L, 2013L, 2013L, 2013L, 2013L,
2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 2014L, 2014L,
2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L
), quarter = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L,
4L, 4L, 4L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L), .Label = c("Q1",
"Q2", "Q3", "Q4"), class = "factor"), Channel = structure(c(1L,
2L, 1L, 1L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 3L,
1L, 3L, 2L, 2L, 2L, 2L, 1L), .Label = c("AAA", "BBB", "CCC"), class = "factor"),
sales = c(2023L, 2231L, 2832L, 1905L, 2057L, 2099L, 2558L,
2302L, 2505L, 2128L, 2163L, 1128L, 1436L, 2169L, 2476L, 1533L,
2114L, 2613L, 1614L, 2884L, 2335L, 1990L, 2187L, 2695L),
units = c(12L, 12L, 18L, 24L, 23L, 11L, 21L, 21L, 21L, 13L,
11L, 25L, 14L, 23L, 19L, 11L, 23L, 21L, 15L, 15L, 11L, 17L,
13L, 14L)), .Names = c("year", "quarter", "Channel", "sales",
"units"), class = "data.frame", row.names = c("1", "2", "3",
"4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
"16", "17", "18", "19", "20", "21", "22", "23", "24"))
How can I do sum of sales and mean of units group by year,quarter,Channel without using any package. (With base R)
The output should be:
year quarter Channel sales units
1: 2013 Q1 AAA 4855 15.0
2: 2013 Q1 BBB 2231 12.0
3: 2013 Q2 AAA 4004 17.5
4: 2013 Q2 BBB 2057 23.0
5: 2013 Q3 AAA 2558 21.0
6: 2013 Q3 BBB 4807 21.0
7: 2013 Q4 AAA 4291 12.0
8: 2013 Q4 BBB 1128 25.0
9: 2014 Q1 CCC 3912 16.5
10: 2014 Q1 AAA 2169 23.0
11: 2014 Q2 BBB 1533 11.0
12: 2014 Q2 CCC 2114 23.0
13: 2014 Q2 AAA 2613 21.0
14: 2014 Q3 CCC 1614 15.0
15: 2014 Q3 BBB 5219 13.0
16: 2014 Q4 BBB 4177 15.0
17: 2014 Q4 AAA 2695 14.0
Here's another base R solution using by
do.call(rbind, by(df, df[, 1:3],
function(x) cbind(x[1, 1:3], sum(x$sales), mean(x$units))))
Or using "split\apply\combine" theory
t(sapply(split(df, df[, 1:3], drop = TRUE),
function(x) c(sumSales = sum(x$sales), meanUnits = mean(x$units))))
Or similarly
do.call(rbind, lapply(split(df, df[, 1:3], drop = TRUE),
function(x) c(sumSales = sum(x$sales), meanUnits = mean(x$units))))
Edit: it seems like df
is of class data.table
(but you for some reason asked for base R solution only), here's how you would do it with your data.table
object
df[, .(sumSales = sum(sales), meanUnits = mean(units)), keyby = .(year, quarter, Channel)]
# year quarter Channel sumSales meanUnits
# 1: 2013 Q1 AAA 4855 15.0
# 2: 2013 Q1 BBB 2231 12.0
# 3: 2013 Q2 AAA 4004 17.5
# 4: 2013 Q2 BBB 2057 23.0
# 5: 2013 Q3 AAA 2558 21.0
# 6: 2013 Q3 BBB 4807 21.0
# 7: 2013 Q4 AAA 4291 12.0
# 8: 2013 Q4 BBB 1128 25.0
# 9: 2014 Q1 AAA 2169 23.0
# 10: 2014 Q1 CCC 3912 16.5
# 11: 2014 Q2 AAA 2613 21.0
# 12: 2014 Q2 BBB 1533 11.0
# 13: 2014 Q2 CCC 2114 23.0
# 14: 2014 Q3 BBB 5219 13.0
# 15: 2014 Q3 CCC 1614 15.0
# 16: 2014 Q4 AAA 2695 14.0
# 17: 2014 Q4 BBB 4177 15.0
you can try this
aggregate(sales~year+quarter+Channel, data=df, FUN = sum) # sum of sale
aggregate(units~year+quarter+Channel, data=df, FUN = mean) # mean of units