Syntax of multiple embedded for loops

From InterSciWiki

Jump to: navigation, search

ATS Stat Consulting Group to me

show details 1:48 PM (1 hour ago) Hi Doug,

It looks like your loops need a few more sets of brackets. For every “for” statement in R, you need a new set of brackets. So your first double loop would look like:

for(i in 1:lensqrt){
 for(j in 1:lensqrt) {
if (v1[i]==igen & n2[i,j]==2) n2[i,j] <- 0
}
}

And your second double loop would look similar, with an additional set of brackets around the multiple expressions you implement after the “if”:

for(i in 1:lensqrt){
for(j in 1:lensqrt) {
if (v1[i]==igen & n2[i,j]==2) {
                         numnodes=numnodes+1
             F1[numnodes] <- i
}
}
}

I checked that this code runs smoothly as is, but you should double check that it is doing what you believe it should be doing.

-Brigid

Brigid Brett-Esborn
Statistical Consulting Group
UCLA Academic Technology Services

Back to: Software: Kinship simulation

Personal tools