gpt4 book ai didi

r - 使用 base R 分组

转载 作者:行者123 更新时间:2023-12-04 11:03:11 28 4
gpt4 key购买 nike

数据集

我有一个包含年、季度、 channel 、销售额、单位列的数据集

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"))

如何在不使用任何包的情况下按年、季度、 channel 计算单位组的销售额和平均值。 (以 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

最佳答案

这是另一个使用 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

关于r - 使用 base R 分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30050768/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com