在R中建立捐助者归属计划

时间:2013-01-15 22:53:51

标签: r

我的非营利组织正在努力了解不同筹款活动的价值。 我的数据看起来像这样:

donor <- data.frame(
                    donor_names=c("Donor1","Donor1","Donor2","Donor3","Donor3"),
                    donor_id=c("0A1","0A1","0A2","0A3","0A3"),
                    amount=c(500,400,300,250,100),
                    date=c("1/5/2012","11/10/2011","1/4/2012","1/14/2012","1/10/2012"),
                    campaign=c("grassroots","2012-annual appeal","grassroots","grassroots","Jan10 Auction"),
                    stringsAsFactors=FALSE
                   )

donor$date <- as.Date(donor$date,format="%m/%d/%Y")

  donor_names donor_id amount       date           campaign
1      Donor1      0A1    500 2012-01-05         grassroots
2      Donor1      0A1    400 2011-11-10 2012-annual appeal
3      Donor2      0A2    300 2012-01-04         grassroots
4      Donor3      0A3    250 2012-01-14         grassroots
5      Donor3      0A3    100 2012-01-10      Jan10 Auction

我需要看看有多少捐款可以归因于每个广告系列,因为捐赠者的所有未来贡献都可以归因于他们首次捐赠的广告系列。

最终结果将返回“2012年度年度申诉”,900份,“草根”300份,“Jan10拍卖”350份。

提前谢谢。

1 个答案:

答案 0 :(得分:4)

library("plyr")

ddply(ddply(donor,
            .(donor_names),
            summarize,
            amount = sum(amount),
            campaign = campaign[date == min(date)]),
      .(campaign),
      summarize,
      amount = sum(amount))

给出了

            campaign amount
1 2012-annual appeal    900
2         grassroots    300
3      Jan10 Auction    350