2

我有如下数据:

> head(ddd)
        id affiliate_id affiliate_account_id source         Pipeline num  good   bad
1 61046463         1006                   69 29eada Contact Info Bad   1 FALSE  TRUE
2 61046770         1006                   69 344f39    Did not Reach   1  TRUE FALSE
3 61053937         1006                   69 fff384               na   1  TRUE FALSE
4 61053941         1006                   69 22d8b6          App Out   1  TRUE FALSE
5 61060137         1006                   69 29eada         No Offer   1  TRUE FALSE
6 61060221         1006                   69 3fdb4f Contact Info Bad   1 FALSE  TRUE

我正在尝试使用 ddply 函数汇总数据,以便获得如下内容:

  affiliate_id affiliate_account_id lead_count good_count bad_count good_rate bad_rate 
1         1006                   69        360       300        60     %        %    
2         1006                 5212         64       60         4      %        %
3         1031                 5102         22       3          20     %        %  
4         1035                 5211          5       15         10     %        %  
5         1035                 5216         90       30         60     %        %

其中百分比 (%) 是好/坏的比率,即affiliate_account_id。

我似乎无法弄清楚如何获得列数并评价食物的好坏。任何人都可以帮助从以下到上表中的最后四列。

ddply(ddd, .(affiliate_id, affiliate_account_id), summarise, lead_count=length(affiliate_id))
4

2 回答 2

7

您可以使用sum来计算逻辑的数量,

ddply(dat, .(affiliate_id, affiliate_account_id), summarise, 
      lead_count=length(affiliate_id),
      good_count= sum(good),
      bad_count = sum(bad),
      good_rate = sum(good)/length(affiliate_id),
      bad_rate = sum(bad)/length(affiliate_id))

 affiliate_id affiliate_account_id lead_count good_count bad_count good_rate  bad_rate
1         1006                   69          3          2         1 0.6666667 0.3333333
2         1006                   70          3          2         1 0.6666667 0.3333333

哪里dat是:(我稍微修改你的输入以获得2个不同的组,因为你只给一个)

       id affiliate_id affiliate_account_id source         Pipeline num  good   bad
1 61046463         1006                   69 29eada Contact Info Bad   1 FALSE  TRUE
2 61046770         1006                   69 344f39    Did not Reach   1  TRUE FALSE
3 61053937         1006                   69 fff384               na   1  TRUE FALSE
4 61053941         1006                   70 22d8b6          App Out   1  TRUE FALSE
5 61060137         1006                   70 29eada         No Offer   1  TRUE FALSE
6 61060221         1006                   70 3fdb4f Contact Info Bad   1 FALSE  TRUE
于 2013-04-19T18:17:52.923 回答
3
ddd$good_count<-with(ddd,ifelse(good=="TRUE",1,0))

ddd$bad_count<-with(ddd,ifelse(bad=="TRUE",1,0))

ddply(ddd, .(affiliate_id, affiliate_account_id), summarise, lead_count=length(affiliate_id), good_count=sum(good_count), bad_count=sum(bad_count),good_rate = sum(good_count)/length(affiliate_id),
  bad_rate = sum(bad_count)/length(affiliate_id))
于 2013-04-19T18:16:08.773 回答