【问题标题】:Aggregate Function in R within FOR loopFOR循环中R中的聚合函数
【发布时间】: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))

标签: r aggregate apply


【解决方案1】:

你需要的是as.formula

df <- read.table("clipboard", header = T)
Columns <- names(df)[!names(df) %in% c("bidvalue_CPMf", "creative_code")]

for (j in Columns){
  fo <- as.formula(paste("bidvalue_CPMf~creative_code+",j))
  print(aggregate(fo,data=df,FUN=mean))
}

如果您只需要使用reqcount, hasbought,hasclick 进行分析。使用

Columns <- c("reqcount", "hasbought", "hasclick")

【讨论】:

    【解决方案2】:

    你可以试试

      nm1 <- names(temp3)[2:4]
      lapply(nm1, function(x) {
        aggregate(temp3['bidvalue_CPMf'], by = c(temp3['creative_code'], temp3[x]),
                         FUN=mean)
       })
    
    [[1]]
       creative_code reqcount bidvalue_CPMf
    1              1        0      175.0000
    2             79        0      700.0000
    3             79        1      300.0000
    4             79        2      176.0000
    5              1        3      300.0000
    6             79        3      684.0000
    7              1        4      300.0000
    8             79        4      271.0000
    9             79        5      265.3333
    10             1        7      179.5000
    11            79        7       11.0000
    12             1        8       65.0000
    13             1        9      176.6667
    
    [[2]]
      creative_code hasbought bidvalue_CPMf
    1             1         0      199.0000
    2            79         0      306.8000
    3             1         1      250.6667
    4            79         1      351.5000
    5             1         2      160.0000
    6            79         2        9.0000
    
    [[3]]
      creative_code numclick bidvalue_CPMf
    1             1        0         208.5
    2            79        0         279.5
    3             1        1         300.0
    4            79        2          13.0
    5             1        3          77.0
    6             1        4         300.0
    7             1        5         159.0
    8            79       23         700.0
    

    使用个别方法检查结果

    aggregate(bidvalue_CPMf~creative_code+hasbought, temp3, FUN=mean)
      creative_code hasbought bidvalue_CPMf
    1             1         0      199.0000
    2            79         0      306.8000
    3             1         1      250.6667
    4            79         1      351.5000
    5             1         2      160.0000
    6            79         2        9.0000
    

    如果数据集很大,可以使用dplyrdata.table

    library(dplyr)
    lapply(nm1, function(x){
                 temp3 %>%
                    group_by_('creative_code',.dots=x) %>% 
                    summarise(bidvalue_CPMf=mean(bidvalue_CPMf))})
    

    使用data.table

    library(data.table)
    setDT(temp3)
    lapply(nm1, function(x) temp3[, .(bidvalue_CPMf=mean(bidvalue_CPMf)) , 
                          c('creative_code', x)]) 
    

    【讨论】:

    • 感谢您不厌其烦地发布如此详细的解释。第一个在我的数据集上运行缓慢,但其他的运行良好。
    猜你喜欢
    • 1970-01-01
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多