[R] Aggregating the matrices
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) How can I achieve my objective? Thank you in advance. Sergey __ R-help@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.
Re: [R] Aggregating the matrices
On Mon, Sep 6, 2010 at 9:56 AM, Sergey Goriatchev serg...@gmail.com 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) How can I achieve my objective? Thank you in advance. Try this: library(plyr) colSums(rbind.fill(as.data.frame(a), as.data.frame(b)), na.rm = TRUE) ES PT Z CF GX ST EO AT EM -75 -2 5 11 2 8 5 4 12 -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@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.
Re: [R] Aggregating the matrices
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@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 __ R-help@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.
Re: [R] Aggregating the matrices
Gabor, David, thank you. David, your last suggestion is what I need. Regards, Sergey On Mon, Sep 6, 2010 at 16:12, David Winsemius dwinsem...@comcast.net wrote: 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@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 -- Kniven skärpes bara mot stenen. __ R-help@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.