简体   繁体   中英

While loop nested in for loop R

I am trying to pull out individual subject data in R using a for and while loop. I would like for the loop to pull the data accordingly and save it as it's own data file. The issue is the for loop not counting Subjects properly and returning the proper value for b. My while loop works perfectly because I can manually set the value of b, run the loop, and produce the correct data files.

Subjects = (1:2)
r = (1);

ST = (d$onReadyTime) #need to get ST to read not just the first number in onReadyTime for each trial 
ST=strsplit(ST,split = "a")

for (b in (1:length(Subjects)){
  b <- Subjects[r]
while (r == Subjects[b])
    {STSubject = (ST[[b]])
    ST2=(STSubject)
    #ST2=`colnames<-`(ST2,Subjects[i])
    write.table(ST2,file = paste("ST_Subject_",b,".csv",sep=""),row.names = FALSE, col.names= TRUE)
    r = r+1
    }
}

Without a minimum working example, only general guidance can be offered.

n <- 0
  for (i in seq_along(models)) {
    for (j in seq_along(meters)) {
      n <- n + 1
      make_line(i, j) -> glances[[n]]
      }
  }

The key point here, as remarked, is to initialize the counter outside the inner loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM