Re: [R] replicate matrix blocks different numbers of times into new matrix

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat, 19 Jul 2008 21:39:25 +0200

Ralph S. wrote:
> Hi,
>
> I am trying to replicate blocks of a matrix (defined by factors) into another matrix, but an unequal, consecutive number of times for each factor.
>
> I need to find an elegant and fast way to do this, so loops will not work.
>
> An example of what I am trying to do is the following:
>
> # the data - first column entries are both data and the two factors
> x<-matrix(c(1,2,3,4),2,2)
>
>> x
>>
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
>
> # the number of repetitions of the first and second factor
> n<-c(1,3)
>
> This is what I want as output:
>
> [,1] [,2]
> [1,] 1 3
> [2,] 2 4
> [3,] 2 4
> [4,] 2 4
>
>
> Any ideas how to get there? I have tried using tapply with combination of rep, but this does not work (I need 1 and then 3 replications).
>
> Any help would be great!
>
Will this do?

x[rep(1:2,n),]

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk)              FAX: (+45) 35327907

______________________________________________
R-help_at_r-project.org 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.
Received on Sat 19 Jul 2008 - 19:42:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 20 Jul 2008 - 00:32:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive