[R] about loop, 10 times, 10 matrix

David Winsemius dwinsemius at comcast.net
Sun Dec 9 18:32:48 CET 2012


On Dec 9, 2012, at 9:06 AM, 歐詠芝 wrote:

> Dear all:
>
> I have simulated ability(500 examinees) and difficulty(40 items).
> as:
> N <- 500
> theta <- rnorm( N )
> I <- 40
> b <- seq( -2 , 2 , len=I )
> p1 <- plogis( outer( theta, b, "-" ) )
>
> I want to simulate 10 responses matrix
> through this syntax: respR <- 1 * ( p1 > matrix( runif( N*I ) ,  
> nrow=N ,
> ncol=I ) )
>
> I tried this:
> n<-10
> respR<-list()
> for(i in 1:n){
>  respR[[i]] <- 1 * ( p1 > matrix( runif( N*I ) , nrow=N , ncol=I ) )
> }
>
> but I didn't get 10 respR matrix, such as: respR[[1]], respR[[2]].....
>
I got 10 logical matrices ... just as the code specified. What were  
you expecting?

 > str(respR[[1]])
  num [1:500, 1:40] 1 1 1 1 1 1 0 1 0 0 ...
 > str(respR)
List of 10
  $ : num [1:500, 1:40] 1 1 1 1 1 1 0 1 0 0 ...
  $ : num [1:500, 1:40] 1 1 0 1 1 1 0 0 1 1 ...
  $ : num [1:500, 1:40] 1 1 1 0 1 1 1 1 1 0 ...
  $ : num [1:500, 1:40] 0 1 1 0 1 1 1 1 1 0 ...
  $ : num [1:500, 1:40] 0 1 0 0 1 1 1 1 1 0 ...
  $ : num [1:500, 1:40] 0 1 1 0 1 1 1 1 0 1 ...
  $ : num [1:500, 1:40] 1 1 1 0 1 1 1 1 1 1 ...
  $ : num [1:500, 1:40] 0 1 1 1 1 1 0 1 1 0 ...
  $ : num [1:500, 1:40] 1 1 1 1 1 1 1 1 1 1 ...
  $ : num [1:500, 1:40] 0 1 1 0 1 1 1 1 1 0 ...

> please help me to resolve that.
>
> thanks!
>
> 	[[alternative HTML version deleted]]

Please learn to configure your mail client to post in plain text.

-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list