R – Storing output from nested for loops in r

nested-loopsoutputrsubmatrix

I feel like I should have found/understood the answer to this by now after frequent searches and lots of forum reading, but it's still really confusing to me. I have two nested for loops in r and need to save the output to a variable but I don't exactly know what to assign and where. The code functions as I want it to, it's just that I can't seem to find how to get an output in one form or another. The input into the loops is a list of submatrices. The output could either be the same format but would include the changes that occurred in the loop, or a more ideal format would include all of the rows and columns in one matrix. I tried doing a cbind as well as creating variables outside of the loops to store everything in later (you'll probably notice my commented-out attempts at these), but, like I said, I'm still a little confused. Any help would be greatly appreciated!

loop.List <- list()
#results.frame <- data.matrix()

ctr <- 0 # creating a counter and zeroing it


for (i in 1:length(subM.List))   { #Looping through each submatrix in the list
  loop.List[[i]] <- list()
  for (j in 2:nrow(subM.List[[i]])){ #Loop through each row of each submatrix in the list
  if ((subM.List[[i]][j, "LAT"] == -180) & #Imputation 1, imputing data points with 0 activity intensity
     (subM.List[[i]][j, "ACTIVITYIN"] ==  0)   & 
     !((subM.List[[i]][j-1, "ACTIVITYIN"]  >  0))[1]) {   #stop imputing at the first occurrence of a value > 0 in activity intensity
        imputeLat <- replace(subM.List[[i]][ ,"LAT"], subM.List[[i]][j,"LAT"], subM.List[[i]][j-1,"LAT"])
        imputeLon <- replace(subM.List[[i]][ ,"LON"], subM.List[[i]][j,"LON"], subM.List[[i]][j-1,"LON"])
        replace.col <- replace(subM.List[[i]][ ,"Impute"], subM.List[[i]][j,"Impute"], 1) #populated impute column. If point is imputed will have a 1
        allComb <- cbind(imputeLat, imputeLon, replace.col)
        ctr <- (ctr + 1)
    }
  }
  #result[[i]] <- allComb
#write.table(results.frame, "C:\\RWorkspace\\newMatrix.txt")
  #return(results.frame)
}

EDIT: data sample of one of the submatrices
The list contains several submatrices that have differing numbers of rows.

FixTYPE          ActivityIn    LAT     LON     Impute
 8                      0   32.81320 -117.2300
 8                      0   32.81324 -117.2301
 8                      1   32.81327 -117.2302
 8                      1   32.81326 -117.2301
 6                      0   32.81324 -117.2300
 6                      0   32.81338 -117.2302
 6                      0   32.81353 -117.2299
 7                      0   -180.000 -180.000
 7                      0   -180.000 -180.000
 7                      0   -180.000 -180.000
 7                      0   -180.000 -180.000
 7                      1   -180.000 -180.000
 7                      2   -180.000 -180.000
 7                      1   -180.000 -180.000
 1                      0   32.81315 -117.2300
 8                      0   32.81318 -117.2300

Best Answer

Similar to how you create a ctr variable outside the scope of your for loop, if you want to retain the results of these loops you should create some sort of storage variable for the output you're trying to capture. If you can provide a reproducible example we can help you more.

Here's a simple example:

p=10
q=20
M=matrix(seq(1,p*q), p, q)

output=matrix(NA, p,q) # storage matrix
for(i in 1:p){
  for(j in 1:q){
    # do something
    output[i,j] = 2*i + j^2
  }  
}

As others have stated, it's likely that what you are trying to accomplish would be simplified by using the apply functions.

Related Topic