-1

我的非营利组织正在努力了解不同筹款活动的价值。我的数据如下所示:

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 Auction”和 350。

先感谢您。

4

1 回答 1

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
于 2013-01-15T23:56:24.913 回答