Kondamani, Arjun (GMI - NY Corporate Bonds)
2007-Dec-19 15:52 UTC
[R] Aggregating by a grouping
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 So I want to get : A1 40 A2 26 How do I use tapply AND the list to get my custom groupings? thx -------------------------------------------------------- This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. -------------------------------------------------------- [[alternative HTML version deleted]]
> 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_namesTry 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}}
try this: apply(sapply(basic_map, function(x)tapply(df$Value, df$Book, sum)[x]), 2, sum) On 19/12/2007, Kondamani, Arjun (GMI - NY Corporate Bonds) <arjun_kondamani at ml.com> wrote:> 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 > > So I want to get : > > A1 40 > A2 26 > > How do I use tapply AND the list to get my custom groupings? > > thx > -------------------------------------------------------- > > This message w/attachments (message) may be privileged, confidential or proprietary, and if you are not an intended recipient, please notify the sender, do not use or share it and delete it. Unless specifically indicated, this message is not an offer to sell or a solicitation of any investment products or other financial product or service, an official confirmation of any transaction, or an official statement of Merrill Lynch. Subject to applicable law, Merrill Lynch may monitor, review and retain e-communications (EC) traveling through its networks/systems. The laws of the country of each sender/recipient may impact the handling of EC, and EC may be archived, supervised and produced in countries other than the country in which you are located. This message cannot be guaranteed to be secure or error-free. This message is subject to terms available at the following link: http://www.ml.com/e-communications_terms/. By messaging with Merrill Lynch you consent to the foregoing. > -------------------------------------------------------- > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O
One possibility is:> x[1] "A" "B" "C" "D" "A" "C" "D" "B"> y[1] 10 11 9 8 12 4 5 7> basic_map[[1]] [1] "A" "B" [[2]] [1] "C" "D"> a <- which(sapply(basic_map,function(u) x %in%u),arr.ind=TRUE)> aggregate(y,list(a[order(a[,1]),2]),sum)Group.1 x 1 1 40 2 2 26>--- "Kondamani, Arjun (GMI - NY Corporate Bonds)" <arjun_kondamani at ml.com> wrote:> 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 > > So I want to get : > > A1 40 > A2 26 > > How do I use tapply AND the list to get my custom > groupings? > > thx >--------------------------------------------------------> > This message w/attachments (message) may be > privileged, confidential or proprietary, and if you > are not an intended recipient, please notify the > sender, do not use or share it and delete it. Unless > specifically indicated, this message is not an offer > to sell or a solicitation of any investment products > or other financial product or service, an official > confirmation of any transaction, or an official > statement of Merrill Lynch. Subject to applicable > law, Merrill Lynch may monitor, review and retain > e-communications (EC) traveling through its > networks/systems. The laws of the country of each > sender/recipient may impact the handling of EC, and > EC may be archived, supervised and produced in > countries other than the country in which you are > located. This message cannot be guaranteed to be > secure or error-free. This message is subject to > terms available at the following link: > http://www.ml.com/e-communications_terms/. By > messaging with Merrill Lynch you consent to the > foregoing. >--------------------------------------------------------> > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >