2

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
Misc
  • 645
  • 1
  • 7
  • 20
  • 1
    This could probably all be done with `lapply` and `apply`, but without access to your data (or a minimal example) it's hard to help specifically – alexwhan Jun 17 '13 at 23:22
  • I'd recommend you write a function `foo` that takes one submatrix as as input and outputs `allComb`. Then run `lapply(subM.List, foo)` or `do.call(rbind, lapply(subM.List, foo))`. – flodel Jun 17 '13 at 23:49

1 Answers1

4

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.

David Marx
  • 8,172
  • 3
  • 45
  • 66
  • Ok, thanks @David Marx, I think I finally get it. And yes, I need to become more comfortable with the apply functions and not utilize loops as much. – Misc Jun 18 '13 at 22:00
  • @David Marx , What if I input a dataframe and want output as a dataframe with both i and j as columns? – Polar Bear Aug 27 '16 at 09:47
  • @PolarBear This sounds like a different question. Can you please post a new topic asking this and provide a demo input and what you expect the output to look like? Link it here and we can move this discussion there. – David Marx Aug 28 '16 at 13:34
  • @DavidMarx Thanks Here it is: http://stackoverflow.com/questions/39190052/how-to-deal-with-the-object-classes-in-loop – Polar Bear Aug 28 '16 at 14:35
  • @PolarBear: "This question was voluntarily removed by its author." – David Marx Aug 29 '16 at 18:10