[R] Collapsing/aggregating columns in a matrix
Ista Zahn
izahn at psych.rochester.edu
Thu Mar 24 13:53:36 CET 2011
Hi Barth,
Use rowMeans() like this:
mat <- matrix(rnorm(100), ncol=10)
cbind(sum1.5 = rowSums(mat[,1:5]), sum2.6 = rowSums(mat[,2:6]))
Best,
Ista
On Thu, Mar 24, 2011 at 12:10 PM, Barth B. Riley <bbriley at chestnut.org> wrote:
> Dear list
>
> I want to know if there is a function in R that enables one to create a new matrix based on values in an existing matrix, whereby each column in the new matrix is a row-specific summary (e.g., sum, mean) of multiple columns in the original matrix. For instance, to take a 12-column matrix and collapse it into a 3 column matrix containing the means for each row of columns 1-4, 5-8, and 9-12 of the original matrix. I want to do this with a large number of data matrices where the columns to collapse varies from dataset to dataset.
>
> Thanks
>
> Barth
>
> PRIVILEGED AND CONFIDENTIAL INFORMATION
> This transmittal and any attachments may contain PRIVILEGED AND
> CONFIDENTIAL information and is intended only for the use of the
> addressee. If you are not the designated recipient, or an employee
> or agent authorized to deliver such transmittals to the designated
> recipient, you are hereby notified that any dissemination,
> copying or publication of this transmittal is strictly prohibited. If
> you have received this transmittal in error, please notify us
> immediately by replying to the sender and delete this copy from your
> system. You may also call us at (309) 827-6026 for assistance.
>
> ______________________________________________
> 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.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list