Something like this could work for you (note that a few other packages would be 
faster, such as data.table), but plyr is intuitive. 

require(plyr)

# example data set
dat <- data.frame(col1 = rep(c("a","b"), each=5),
col2 = rep(c("prog1","prog2","prog3","prog4","prog5"), 2), 
col3 = rnorm(10), col4 = rnorm(10))

ddply(dat, .(col1), summarise, 
progs = paste(col2,collapse=","),
sumcol3 = sum(col3),
sumcol4 = sum(col4))

______________________Scott Chamberlain
Rice University, EEB Dept.


On Thursday, June 16, 2011 at 6:55 PM, Will Parbury wrote:

> 
> Hi R Help list
> 
> I'm looking to visualise US foreign aid 1946-2009 and I have the dataset for 
> this. The trouble is it's a bit too complex and I need to simply it 
> 
> I want to merge all of the rows with the same country together and add up the 
> individual totals to make one total figure per country per year
> 
> Below is an example of the kind of data. The real dataset has 2447 rows and 
> covers 63 years. There are many missing values. Basically I would 
> like to go from the top example to the bottom example and be able to scale it 
> up to cover many more countries and years.
> 
> Thanks in advance for any help you can offer.
> 
> country programme FY1985 FY1986
> Afghanistan Child Survival and Health 10,000,000 15,000,000
> Afghanistan Department of Defense Security Assistance 15,000,000 15,000,000
> Afghanistan Narcotics Control NA 5,000,000 
> Afghanistan Peace Corps NA 7,500,000
> Albania Child Survival and Health NA 25,000
> Albania Department of Defense Security Assistance NA NA
> Albania Narcotics Control NA 5,000
> Albania Peace Corps NA NA
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> 
> country programme FY1985 FY1986
> Afghanistan Child Survival and Health,Department of Defense Security 
> Assistance,Narcotics Control,Peace Corps 25,000,000 42,500,000
> Albania Child Survival and Health,Department of Defense Security 
> Assistance,Narcotics Control,Peace Corps NA 30,000
> 
> 
>  [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help@r-project.org (mailto:R-help@r-project.org) mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to