[R] problems with nested loop

Matthew Keller mckellercran at gmail.com
Tue Sep 18 17:40:11 CEST 2007


hi Riddle,

You subscript is out of bounds because this line:
  for (j in seq(20,100,20)){

is incorrect - it is trying to index the 20th, 40th, 60th... column of
a matrix that has only 5 columns. Try for (j in 1:5) instead. I'm not
sure what the purpose of your script is so can't comment on what else
needs to be done...

Matt


On 9/17/07, Riddle Chin <riddle_chin at yahoo.com> wrote:
> Hi, everyone:
>      R is new to me. I am writing a nested loop to simulate data for t-test. The following code is wrong. The subscript is out of bounds. Could anyone tell me how to revise it? Thanks, Riddle Chin.
>
>   result<-matrix(ncol=5, nrow=1000)
> colnames(result)<-c('N=20','N=40','N=60','N=80','N=100')
>  for (i in 1:1000){
>    for (j in seq(20,100,20)){
>      x<-rnorm(j,2.7,1)
>      result[i,j]<-t.test(x,mu=4)$p.value<=0.05
>    }
>  }
>
>
>
> ---------------------------------
> Luggage? GPS? Comic books?
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>


-- 
Matthew C Keller
Postdoctoral Fellow
Virginia Institute for Psychiatric and Behavioral Genetics



More information about the R-help mailing list