【问题标题】:Subsetting while summarizing data with ddply使用 ddply 汇总数据时的子集
【发布时间】:2013-04-19 17:55:34
【问题描述】:

我的数据看起来像:

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

【问题讨论】:

    标签: r plyr


    【解决方案1】:

    你可以使用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
    

    【讨论】:

      【解决方案2】:
      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))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多