1

I am looking for a method to divide a nrow(x) in a combined for loop and llply in to chunks of 20 until the internal loop reaches the max length of nrow.

Below is the code I am using to do this but fail to add the code to divide nrow into bulks of 20.

The UD_spek13 is a list of UDs from the move pakage.

Maybe my code can be written in a more memory-friendly way?

How would I re-write the code?

for (i in 3:length(UD_spek13)) {
  llply(unlist(UD_spek13[i]), function(x) move.contour(x, range.subset=**code to devide nrow into bulks of 20**,ts=1,ras=10,lev=c(50,95), le=20,
                                                crs=crs, name=paste(x@DBMvar@idData$trackId, "", sep ="_"), 
                                                path="C:/Users/Winmac/Documents/article3"))
}

This is what I want to acgieve (code is not working)

for (i in 3:length(UD_spek13)) {
  llply(unlist(UD_spek13[i]), function(x) move.contour(x, range.subset=**seq(4, nrow(x), by = 20)**,ts=1,ras=20,lev=c(50,95), le=20,
                                                crs=crs, name=paste(x@DBMvar@idData$trackId, "", sep ="_"), 
                                                path="C:/Users/Winmac/Documents/article3test"))
}

range.subset=seq(4, nrow(x), by = 20)

  • Maybe `(1:nrow(myData)) %/% 20` ? – zx8754 Aug 15 '16 at 09:24
  • your code does divide the nrow into chunks of 20 but I need the code to run from 4:20, 21:40, 41:60 and so on. Maybe I must create llply or lapply loop at range.subset= ? – Nicolai Jørgensen Aug 15 '16 at 09:31
  • 4:20 is not 20 is it, we can change it to `(4:nrow(mtcars)) %/% 20`? Please provide [reproducible example?](http://stackoverflow.com/questions/5963269). Yes, I think you will need double loop, 1 loop through UD_spek13 list, then another to loop through bulks, we can use split within 2nd loop and use my code. – zx8754 Aug 15 '16 at 09:34
  • I see. How would you suggest the double loops should look like? Using mtcars as an example? – Nicolai Jørgensen Aug 15 '16 at 09:46

1 Answers1

1

Using mtcars, we can do following double loop:

#dummy list
myList <- list(mtcars[1:16,], mtcars[17:32,])

nBulk <- 5 #in your case this number is 20

lapply(myList, function(i){
  #exclude first 3 rows
  df1 <- tail(i, -3)
  #create bulks
  df1$group <- (1:nrow(df1)) %/% nBulk
  lapply(split(df1, df1$group), function(j){
    # some code on bulks, here just dividing by 2
    j/2
  })
})
zx8754
  • 52,746
  • 12
  • 114
  • 209