【发布时间】:2015-06-22 11:48:32
【问题描述】:
我有一个数据表(temp3),就像(原始表有大约 100 万行)-
creative_code reqcount hasbought numclick FeedbackCPM bidvalue_CPMf browser
79 5 1 0 19 9 C
1 0 0 0 39 50 C
79 3 1 0 1205 684 C
1 7 1 5 82 159 C
1 9 0 3 15 77 C
79 5 0 0 1575 700 C
1 0 0 0 95 300 C
1 4 1 4 95 300 C
1 3 0 0 1 300 C
1 8 0 0 30 65 C
1 9 1 0 17 293 C
1 4 0 1 140 300 IE
79 4 0 0 838 271 F
79 7 1 2 0 13 C
1 9 2 0 67 160 C
79 2 0 0 268 176 F
79 0 1 23 1634 700 C
79 1 0 0 0 300 C
79 5 0 0 143 87 C
79 7 2 0 0 9 IE
1 3 0 0 178 300 IE
1 7 0 0 111 200 F
我需要的是对所有具有 reqcount、hasbought、hasclick 的 Creative_code 分别表示的意思。我可以通过使用命令分别找到 Creative_code+reqcount 的平均值 - 聚合(bidvalue_CPMf~creative_code+reqcount,data=temp3,FUN=mean)
但是,如果我使用以下代码,我会收到错误 -
Code -
for (j in names(temp3)) aggregate(bidvalue_CPMf~creative_code+j,data=temp3,FUN=mean)
Error - Error in model.frame.default(formula = bidvalue_CPMf ~ creative_code + : variable lengths differ (found for 'j')
请帮忙。
【问题讨论】:
-
您可以尝试列表方法而不是公式。除了
creative_code之外还有哪些分组变量? -
Creative_code 是一个分组变量,另一个变量会不断变化。第一种情况是 Creative_code+reqcount,第二种情况是 Creative_code+hasbought,以此类推。关于第二点,我打算将 for 循环的输入作为 for (j in c('reqcount',hasbought'.... 等等。
-
在你的代码中你有
names(temp3),这意味着所有的列名。但是您打算按所有列名分组吗? -
使用前四列作为分组变量
nm1 <- names(temp3)[1:4];lapply(nm1, function(x) aggregate(temp3['bidvalue_CPMf'], cbind(temp3['creative_code'], temp3[x]), FUN=mean))