[R] Replacing sets of rows in matrix within a loop

nqf n.j.franklin at pgr.reading.ac.uk
Wed Jun 27 20:01:34 CEST 2012


Dear R-help,

I am writing some simulation code to create multiple sets of time-to-event
clinical trial data (for use in meta-analysis). Within each trial, I want to
apply censoring via simulation of uniform variables (with minimum zero and
maximum the median outcome time for that particular trial).

I have started by pre-allocating a matrix which has 3 columns; one for trial
number, one for patient number, and I want to complete the third with the
results of the uniform simulation.

For example, I have :
n<-10 ## patients per trial
trials<-3 ## 3 trials
outcome<-matrix(NaN,nrow=n*trials,ncol=3)
 for(i in 1:trials){
 outcome[,1]<-rep(1:trials,each=n)
 outcome[,2]<-rep(1:n)
 }

In the third column, for each trial I want to create n sets of random draws:
runif(n,0,median[i]), where median[i] is the median outcome time in that
trial.
median<-as.matrix(c(2,4,3),)   ## for example

So far I have been trying to include the following within the for loop:
outcome[,3]<-runif(n,0,median[i])
But this just repeats the uniform draws from the last iteration of the loop
in column 3.

Can anyone advise how to replace the third column at each iteration rather
than repeating? I think I can use rbind, but will this slow down my
simulation?

Many thanks in advance for your help. 

Natalie

--
View this message in context: http://r.789695.n4.nabble.com/Replacing-sets-of-rows-in-matrix-within-a-loop-tp4634658.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list