[R] create an array with rep

Dimitris Rizopoulos Dimitris.Rizopoulos at med.kuleuven.be
Sun Jul 22 13:35:09 CEST 2007


try this:

x <- -3:3
as.matrix(expand.grid(x, x))
# or
t(as.matrix(expand.grid(x, x)))


I hope it helps.

Best,
Dimitris


----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
      http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting baptiste Auguié <ba208 at exeter.ac.uk>:

>
> Hi,
>
>
> I want to make the following array of numbers:
>
> -3 -3 -3 -3 -3 -3 -3 -2 -2 -2 -2 -2 -2 -2 ...  3  3  3  3  3  3  3
> -3 -2 -1  0  1  2  3 -3 -2 -1  0  1  2  3 ... -3 -2 -1  0  1  2  3
>
> (3 would be N, a painful example to type number).
>
> Here is my dirty attempt to do it,
>
>> N<-3
>>
>> x<-c(-N:N)
>>
>> rj<-rbind(matrix(outer(matrix(1,1,2*N+1),x),nrow=1),rep(x,2*N+1))
>>
>
> It sort of works, but I'd like some advice on how to do it properly
> as I'm very new to R and N may be big at some point.
>
> Thanks,
>
> baptiste
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>
>



Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



More information about the R-help mailing list