[R] Aggregating by a grouping

Henrique Dallazuanna wwwhsd at gmail.com
Wed Dec 19 17:44:38 CET 2007


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



More information about the R-help mailing list