使用基数R分组

时间:2015-05-05 10:42:19

标签: r

数据集

我有一个包含列年份,季度,渠道,销售额,单位

的数据集
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"))

如何在不使用任何套餐的情况下按年份,季度,渠道分组销售额和平均值。 (用基数R)

输出应为:

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

2 个答案:

答案 0 :(得分:3)

你可以试试这个

aggregate(sales~year+quarter+Channel, data=df, FUN = sum) # sum of sale
aggregate(units~year+quarter+Channel, data=df, FUN = mean) # mean of units

答案 1 :(得分:3)

这是使用by

的另一个基础R解决方案
do.call(rbind, by(df, df[, 1:3], 
                  function(x) cbind(x[1, 1:3], sum(x$sales), mean(x$units))))

或使用“ split \ apply \ combine ”理论

t(sapply(split(df, df[, 1:3], drop = TRUE), 
                   function(x) c(sumSales = sum(x$sales), meanUnits = mean(x$units))))

或类似地

do.call(rbind, lapply(split(df, df[, 1:3], drop = TRUE), 
                     function(x) c(sumSales = sum(x$sales), meanUnits = mean(x$units))))

编辑:似乎df属于data.table类(但由于某种原因,您只需要基础R解决方案),以下是您如何使用data.table对象

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