[R] Aggregating the matrices

David Winsemius dwinsemius at comcast.net
Mon Sep 6 16:12:56 CEST 2010


On Sep 6, 2010, at 9:56 AM, Sergey Goriatchev wrote:

> Hello everyone.
>
> Say we have the following:
>
> a <- matrix(c(-75, 3, 5, 9, 2, 3, 5), nrow=1, dim=list("06092010",
> c("ES", "PT", "Z ", "CF", "GX", "ST", "EO")))
> b <- matrix(c(-5, 2, 4, 12, 5), nrow=1, dim=list("06092010", c("PT",
> "CF", "AT", "EM", "ST")))
> d <- cbind(a, b)
>
> I want to calculate sums of the columns that have similar column names
> and then output this summary
> What I want to have is an array that looks like:
>
> ES  PT Z  CF...
> -75  -2  5  11...
>
> I tried the following, but it did not work:
> aggregate(d, list(colnames(d)), sum)

ES is not in the duplicated column names so perhaps your English  
specification is not what you meant:
 > d
           ES PT Z  CF GX ST EO PT CF AT EM ST
06092010 -75  3  5  9  2  3  5 -5  2  4 12  5

 > dupled <- colnames(d)[duplicated(colnames(d))]
 > sapply(dupled, function(x) sum( d[, x]))
PT CF ST
  3  9  3

If you wanted simple a sum over unique column names then it would have  
been somewhat simpler (no need to construct a duplicated set):

 > sapply(unique(colnames(d)), function(x) sum( d[, x]))
  ES  PT  Z   CF  GX  ST  EO  AT  EM
-75   3   5   9   2   3   5   4  12

>
> How can I achieve my objective?
>
> Thank you in advance.
>
> Sergey
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list