[R] Aggregating by a grouping

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Wed Dec 19 17:36:40 CET 2007


> Suppose I have:
> 
> Book   Value 
> A   10 
> B   11 
> C   9 
> D   8 
> A   12 
> C   4 
> D   5 
> B   7 
> 
> I want to summarize above not by Book but by groupings of Books as in
> (below)
> 
> I have a list ... basic_map <- list(c("A",B"),c("C,D"))
> Big_names <- c("A1", "A2")
> Names(basic_map) <- big_names

Try this:

testdf <- data.frame(book=factor(c("A", "B", "C", "D", "A", "C", "D", 
"B")),  value=c(10,11,9,8,12,4,5,7))
bookgroup <- rep("A1", nrow(testdf))
bookgroup[testdf$book=="C" | testdf$book=="D"] <- "A2"
tapply(testdf$value, bookgroup, sum)

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list