Edited to reflect the need for the X matrices: Create a list of 20 + 20 items with your simulation data, and name the members accordingly:
kitem<-10
N<-100
disc<-rnorm(kitem,0,1) # not ( k, ... )
diff=rnorm(kitem,0,1) # not ( k, ... )
pexp <- 1 # ??? - not needed here
# the list that takes all the produced data
mySim <- as.list( NULL )
# function definition reduced to the necessary
irtp <- function( t, a, b ) { 1 / ( 1 + exp( -b * ( t -a ) ) ) }
for( iter in 1:20 )
{
# create two matrices to be filled later
X<-mat.or.vec(N,kitem)
P<-mat.or.vec(N,kitem)
# create and name the theta component
theta = mySim[[ iter ]] <- rnorm( N, 0, 1 )
names( mySim )[ iter ] <- paste ( "theta", iter, sep ="" )
# fill and save the matrices
for( i in 1:N )
{
for( k in 1:kitem )
{
P[i,k]<-irtp(theta[i],diff[k],disc[k] ) # don"t need this: ,pexp)
X[i,k]<-ifelse(runif(1)<P[i,k],1,0)
}
}
mySim[[ 20 + iter ]] <- X
names( mySim )[ 20 + iter ] <- paste ( "X", iter, sep ="" )
}
You can save the list altogether as an R object, if you want that.
Now you can adress each simulation be name:
head( mySim$theta3 )
[1] 0.96068066 0.01966067 -1.25682531 -0.15128916 -0.75950710 -1.22243883
You can add matrices, dataframes etc. to the list
mySim$tau1 <- c( "lists", "take", "everything" )
You can selectively save list members with the corresponding file name:
filename <- paste( names( mySim )[3], ".txt", sep = "" )
write.table( mySim$theta3, filename )
Is that what you had in mind?