[R] Urgent query about R!

David Winsemius dwinsemius at comcast.net
Wed Mar 23 04:40:05 CET 2011


On Mar 22, 2011, at 6:41 PM, Rachel Chu wrote:

> Hi there,
>
> I am currently working on a R programming project and got stuck.
> I am supposed to generate a set of possibilities of 1296 different
> combinations of 4 numbers, ie. 1111, 1234, 2361, (only contain 1 to  
> 6) in a
> matrix form
> here is what I got which has not been working as it keeps coming out  
> with
> the same number on the row..
>
> The code:
> gl1<- gl(1296,1,length=1296, labels=1:1296, ordered=true)
> s<-matrix(gl1, nrow=1296,ncol=4)
>
> and i want to get a result which will provide me 1296 outcomes of  
> DIFFERENT
> COMBINATIONS of numbers (from 1 to 6)
> e.g.
> [1] 1111
> ...
> [1295] 6665
> [1296] 6666
>
> if anyone could email me back with what I am doing wrong and what I  
> should
> actually do i would be extremely thankful!
> (I am doing my project and am so stressed because of getting stuck  
> on the
> supposedly easier problem..)
>

You could try (after fixing the incorrect parameter to ordered) :

as.numeric( apply(s, 1, paste, collapse="") )

If you want to force that vector back to a matrix you could use dim()

 > s2 <- as.numeric( apply(s, 1, paste, collapse="") )
 > dim(s2) <-  c(1296,1)
 > head(s2)
      [,1]
[1,] 1111
[2,] 2222
[3,] 3333
[4,] 4444
[5,] 5555
[6,] 6666



> Thanks again!
>
> xoxo
>
> 	[[alternative HTML version deleted]]


David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list