[R] Aggregating by a grouping
Moshe Olshansky
m_olshansky at yahoo.com
Thu Dec 20 13:29:42 CET 2007
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.
>
More information about the R-help
mailing list