Re: [R] Collapsing/aggregating columns in a matrix

From: Ista Zahn <izahn_at_psych.rochester.edu>
Date: Thu, 24 Mar 2011 12:53:36 +0000

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

______________________________________________
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.
Received on Thu 24 Mar 2011 - 12:56:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Mar 2011 - 13:50:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive