Skip to content

R-dvel [robustness Simulation study of 2 sample test on several combination of factors ]

1 message · tan sj

#
Hi, i think i have figured the purpose of using this index (i-1)*5+j in the previous example that you gave.

It is because that i have to consider the outer loop and inner loop also... so the iterative for i need to minus one because it have ran one times simulation already ,then times the number of sizes of inner loop, then plus the iterative of j....

then for the simulation, i think there will be ((ss-1)*nsds +sim) for the index in the body of for loop...
Is it correct?

Sent from my phone
On Jim Lemon <drjimlemon at gmail.com>, Apr 6, 2016 6:52 PM wrote:
You are running through two loops and putting the output into a
vector. Without this calculation you will overwrite the same elements
of the output vector instead of advancing through it. Try this
example:

# like your 54 element vector of calculations for one condition
testmat1<-rep(0,25)
testmat2<-rep(0,25)
# now try to fill it with the inner loop index
for(i in 1:5) {
 for(j in 1:5) {
  testmat1[j]<-i+j
 }
}
# do it again using a calculation similar to your question
for(i in 1:5) {
 for(j in 1:5) {
  testmat2[(i-1)*5+j]<-i+j
 }
}
testmat1
testmat2

Try out some other things with this. It will give you an idea of how
to index elements of vectors. You could also use a matrix and then
convert it into a vector using as.vector:

testmat3<-matrix(0,nrow=5,ncol=5)
for(i in 1:5) {
 for(j in 1:5) {
  testmat3[i,j]<-i+j
 }
}
as.vector(testmat3)

Jim
On Wed, Apr 6, 2016 at 8:04 PM, tan sj <sj_style_1125 at outlook.com> wrote: