[R] create an array with rep

baptiste Auguié ba208 at exeter.ac.uk
Sun Jul 22 14:10:09 CEST 2007


Thank you all!

This looks like the fastest solution, N being big and my machine slow  
I'll go for this one.

Thanks,

baptiste

On 22 Jul 2007, at 12:54, Gavin Simpson wrote:

>>
>> 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).
>
> This does what you want:
>
> foo <- function(N) {
>    reps <- 2*N+1
>    matrix(c(rep(-N:N, each = reps), rep(-N:N, times = reps)),
>           nrow = 2, byrow = TRUE)
> }



More information about the R-help mailing list