[R] How sum all possible combinations of rows, given 4 matrices

Estigarribia, Bruno estigarr at email.unc.edu
Tue May 28 14:36:29 CEST 2013

Thank you, Arun, I'll try it and let the list know if it works so the
thread can be closed. I haven't worked in R for several years so I am
really rusty.
FYI, for people interested in why I want to do this, I am trying to
calculate all the possible outcomes of applying the model below to a
bilingual corpus:
Deuchar, Margaret, Pieter Muysken, Sung-Lan Wang. 2007. ³Structured
Variation in Codeswitching:Towards an Empirically Based Typology of
Bilingual Speech Patterns.²International Journal of Bilingual Education
and Bilingualism 10 (3):
I accept responsibility for not having included this information in the
original post.

Bruno Estigarribia
Assistant Professor of Spanish, Department of Romance Languages and
Research Assistant Professor of Psychology, Cognitive Science Program
Affiliate Faculty, Global Studies
Dey Hall, Room 332, CB# 3170
University of North Carolina at Chapel Hill
estigarr at email.unc.edu

On 5/27/13 1:54 PM, "arun" <smartpink111 at yahoo.com> wrote:

>Not sure if this is what you expected:
>mat1<- matrix(sample(1:20,3*4,replace=TRUE),ncol=3)
>mat2<- matrix(sample(1:25,3*6,replace=TRUE),ncol=3)
>mat3<- matrix(sample(1:35,3*8,replace=TRUE),ncol=3)
>mat4<- matrix(sample(1:40,3*10,replace=TRUE),ncol=3)
>colnames(matNew)<- (seq(12)-1)%%3+1
>tion(x) rowSums(datNew[,x]))
>#[1] 1920    3
> head(res)
>#     X1 X2 X3
>#[1,] 46 63 70
>#[2,] 45 68 59
>#[3,] 55 55 66
>#[4,] 51 65 61
>#[5,] 48 84 75
>#[6,] 47 89 64
>----- Original Message -----
>From: "Estigarribia, Bruno" <estigarr at email.unc.edu>
>To: "r-help at R-project.org" <r-help at r-project.org>
>Sent: Monday, May 27, 2013 11:24 AM
>Subject: [R] How sum all possible combinations of rows, given 4 matrices
>Hello all,
>I have 4 matrices with 3 columns each (different number of rows though). I
>want to find a function that returns all possible 3-place vectors
>corresponding to the sum by columns of picking one row from matrix 1, one
>from matrix 2, one from matrix 3, and one from matrix 4. So basically, all
>possible ways of picking one row from each matrix and then sum their
>columns to obtain a 3-place vector.
>Is there a way to use expand.grid and reduce to obtain this result? Or am
>I on the wrong track?
>Thank you,
>PS:I believe I have given all relevant info. I apologize in advance if my
>question is ill-posed or ambiguous.
>R-help at r-project.org mailing list
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list