From: Ralph S. <ruffel1_at_hotmail.com>

Date: Sun, 20 Jul 2008 04:39:42 +0000

>> # determine the row numbers of each of the factors

*>> x.row <- split(seq(nrow(x)), x[,1])
*

*>> # process the data and replicate the rows
*

*>> result <- lapply(seq_along(x.row), function(.fact){
*

>> Actually not quite - my mistake, since I oversimplified the problem I have.

>> Here is a more realistic x matrix (plus some additional information):

>> # the data

*>> x<-matrix(c(1,1,2,1,3,4),3,2)
*

>> # number of factors

*>> n_f<-2
*

>> # number of rows taken by each factor

*>> f_length <- c(2,1)
*

>> # number of repetitions I want for the first and second factor

*>> # actually, always the same factor of expansion: both factors are to be replicated n times
*

*>> n<-c(2,2)
*

>> I want something like

>> [6,] 2 4

>> but it is only easy to get

*>> [,1] [,2]
*

*>> [1,] 1 1
*

*>> [2,] 1 1
*

*>> [3,] 1 3
*

*>> [4,] 1 3
*

*>> [5,] 2 4
*

*>> [6,] 2 4
*

>> I am not sure about the first target matrix.

>> I could loop through each level of the factor, use a "which(x[,1]==f[k]" to get the row indices for each factor f[k] and then replicate those indices n times and append them to the result for the previous level of the factor. This does not seem efficient, given that I actually have a large matrix with more than 600 factors.

>> Sorry for the initial misspecification - any ideas how I could solve my problem?

>> Best,

>> Ralph

>> [[elided Hotmail spam]]

>> family_safety_072008

*>> ______________________________________________
*

*>> 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.
*

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 Sun 20 Jul 2008 - 04:44:22 GMT

Date: Sun, 20 Jul 2008 04:39:42 +0000

yes, this works, thank yo. very smart solution!

-R

> Date: Sat, 19 Jul 2008 19:55:58 -0400 > From: jholtman_at_gmail.com > To: ruffel1_at_hotmail.com > Subject: Re: [R] replicate matrix blocks different numbers of times into new matrix > CC: r-help_at_r-project.org > > Will this do it: >

>> # determine the row numbers of each of the factors

> + x[rep(x.row[[.fact]], n[.fact]),] > + })

>> do.call(rbind, result)

> [,1] [,2] > [1,] 1 1 > [2,] 1 3 > [3,] 1 1 > [4,] 1 3 > [5,] 2 4 > [6,] 2 4 >> > > > On Sat, Jul 19, 2008 at 7:17 PM, Ralph S. wrote: >>

>> Actually not quite - my mistake, since I oversimplified the problem I have.

>>

>> Here is a more realistic x matrix (plus some additional information):

>>

>> # the data

>>

>> # number of factors

>>

>> # number of rows taken by each factor

>>

>> # number of repetitions I want for the first and second factor

>>

>> I want something like

>>>> [5,] 2 4

>> [,1] [,2]>> [1,] 1 1>> [2,] 1 3>> [3,] 1 1>> [4,] 1 3

>> [6,] 2 4

>>

>> but it is only easy to get

>>

>> I am not sure about the first target matrix.

>>

>> I could loop through each level of the factor, use a "which(x[,1]==f[k]" to get the row indices for each factor f[k] and then replicate those indices n times and append them to the result for the previous level of the factor. This does not seem efficient, given that I actually have a large matrix with more than 600 factors.

>>

>> Sorry for the initial misspecification - any ideas how I could solve my problem?

>>

>> Best,

>>

>> Ralph

>>

>> ----------------------------------------

>>> Date: Sat, 19 Jul 2008 21:39:25 +0200 >>> From: p.dalgaard_at_biostat.ku.dk >>> To: ruffel1_at_hotmail.com >>> CC: r-help_at_r-project.org >>> Subject: Re: [R] replicate matrix blocks different numbers of times into new matrix >>> >>> 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). >>>>

>> [[elided Hotmail spam]]

>>>> >>> 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 >>> >>

>> _________________________________________________

>> _________________________________________________________________

>> >>

>> family_safety_072008

>> > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? _________________________________________________________________

enger2_072008

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 Sun 20 Jul 2008 - 04:44:22 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 - 08:31:50 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.
*