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

Rui Barradas ruipbarradas at sapo.pt
Thu Jun 28 10:40:26 CEST 2012


Hello,

You are replacing the values of runif each time through the loop. And 
it's not just that, every time through, you are setting outcome[, 1] and 
outcome[, 2] to the same values. Simply put, the loop is not needed.
Corrected:



n <- 100 ## patients per trial
trials <- 3 ## 3 trials
med <- as.matrix(c(2,4,3),)   ## for example

set.seed(123)

outcome <- matrix(nrow=n*trials, ncol=3)
outcome[, 1] <- rep(1:trials, each=n)
outcome[, 2] <- rep(1:n, trials)
outcome[, 3] <- runif(n*trials, 0, rep(med, each=n))

boxplot(outcome[, 3]~outcome[, 1])  # to see what we have


As you can see from the graph, the medians are now half of the values in 
'med'. The name of this variable is misleading, it should be 'maxv' for 
maximum value.
I've changed the name of 'median' to 'med', it already is an R function 
name. It's misleading for a second reason.


Note also that there is another way of making 'trials' and 'n' vary. It 
would change the columns order.

outcome <- expand.grid(1:n, 1:trials) # one instruction.
outcome <- cbind(outcome, runif(n*trials, 0, rep(med, each=n)))
colnames(outcome) <- c("patient", "trial", "value")

Simpler, no?

Hope this helps,

Rui Barradas

Em 27-06-2012 19:01, nqf escreveu:
> 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.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list