Bert, That will result in a data frame, rather than a matrix:
> str(expand.grid(rep(list(c(-1, 1)), 4))) `data.frame': 16 obs. of 4 variables: $ Var1: num -1 1 -1 1 -1 1 -1 1 -1 1 ... $ Var2: num -1 -1 1 1 -1 -1 1 1 -1 -1 ... $ Var3: num -1 -1 -1 -1 1 1 1 1 -1 -1 ... $ Var4: num -1 -1 -1 -1 -1 -1 -1 -1 1 1 ... - attr(*, "out.attrs")=List of 2 ..$ dim : int 2 2 2 2 ..$ dimnames:List of 4 .. ..$ Var1: chr "Var1=-1" "Var1= 1" .. ..$ Var2: chr "Var2=-1" "Var2= 1" .. ..$ Var3: chr "Var3=-1" "Var3= 1" .. ..$ Var4: chr "Var4=-1" "Var4= 1" - attr(*, "colnames")= chr "Var1" "Var2" "Var3" "Var4" Nameeta asked for a matrix, hence the coercion in my reply. Best regards, Marc On Mon, 2006-05-08 at 14:59 -0700, Berton Gunter wrote: > expand.grid(rep(list(c(-1, 1)), 4)) suffices I believe. > > -- Bert Gunter > Genentech Non-Clinical Statistics > South San Francisco, CA > > > > -----Original Message----- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Marc > > Schwartz (via MN) > > Sent: Monday, May 08, 2006 2:50 PM > > To: Nameeta Lobo > > Cc: [email protected] > > Subject: Re: [R] Non repetitive permutations/combinations of elements > > > > On Mon, 2006-05-08 at 16:32 -0500, Nameeta Lobo wrote: > > > Hello all, > > > > > > I am trying to create a matrix of 1s and -1s without any > > repetitions for a > > > specified number of columns. > > > e.g. 1s and -1s for 3 columns can be done uniquely in 2^3 ways. > > > -1 -1 -1 > > > -1 -1 1 > > > -1 1 -1 > > > -1 1 1 > > > 1 -1 -1 > > > 1 -1 1 > > > 1 1 -1 > > > 1 1 1 > > > and for 4 columns in 2^4 ways and so on. > > > > > > I finally used the function combn([0 1],3) that I found at > > the following link > > > > > http://www.mathworks.com/matlabcentral/fileexchange/loadFile.d > > o?objectId=7147&objectType=FILE > > > written by Jos van der Geest in Matlab which generated the above. > > > > > > > > > How can I do this is R? I have looked at permn and combn in > > the combinat library > > > and permutations and combinations in the gtools library and > > I am still confused > > > as to how to get it to work. > > > > > > Any suggestions will be truly appreciated. > > > > > > Thank you > > > > > > Nameeta > > > > > > > With just two elements in the source vector, it may be easiest to just > > use expand.grid() and coerce the result to a matrix: > > > > > as.matrix(expand.grid(rep(list(c(-1, 1)), 3))) > > Var1 Var2 Var3 > > 1 -1 -1 -1 > > 2 1 -1 -1 > > 3 -1 1 -1 > > 4 1 1 -1 > > 5 -1 -1 1 > > 6 1 -1 1 > > 7 -1 1 1 > > 8 1 1 1 > > > > Just adjust the final value of '3' to the number of columns that you > > wish to have: > > > > > as.matrix(expand.grid(rep(list(c(-1, 1)), 4))) > > Var1 Var2 Var3 Var4 > > 1 -1 -1 -1 -1 > > 2 1 -1 -1 -1 > > 3 -1 1 -1 -1 > > 4 1 1 -1 -1 > > 5 -1 -1 1 -1 > > 6 1 -1 1 -1 > > 7 -1 1 1 -1 > > 8 1 1 1 -1 > > 9 -1 -1 -1 1 > > 10 1 -1 -1 1 > > 11 -1 1 -1 1 > > 12 1 1 -1 1 > > 13 -1 -1 1 1 > > 14 1 -1 1 1 > > 15 -1 1 1 1 > > 16 1 1 1 1 > > > > > > HTH, > > > > Marc Schwartz > > ______________________________________________ [email protected] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
