Re: Replicating Matricies...

Alan Zaslavsky (zaslavsk@hcp.med.harvard.edu)
Fri, 16 Jan 1998 23:14:42 -0500 (EST)


Here is a little function (or you can use this code inline in your
program):

dup.alt.rows_function(mat)
{
n<-nrow(mat)
mat[rep(1:n,rep(1:2,length=n)),]
}

You could replace the 1:2 with any pattern of duplications or make it an
argument. By the way, I think Ted Wright's solution missed your desire to
only duplicating the even numbered rows.

On Fri, 16 Jan 1998, Chris Moyer wrote:

> Does anyone know how to replicate every other line in a matrix in
> Splus 4 without using a for() loop, e.g.,
>
> from:
>
> x1 x2 x3
> 1 1 10 15
> 2 2 11 16
> 3 3 12 17
> 4 4 13 18
> 5 5 14 19
> .
> .
> .
>
> to:
>
> x1 x2 x3
> 1 1 10 15
> 2 2 11 16
> 3 2 11 16
> 4 3 12 17
> 5 4 13 18
> 6 4 13 18
> 7 5 14 19
> .
> .
> .
>
> I know there must be a simple solution, but my novice skills are
> holding me back.
>
> Thanks for help in advance.
>
> Chris Moyer
>