Search code examples
rnested-loops

R expand and process data with nested for loops tail


I need to expand some data and then restrict which data remains through tail.

Example of data:

list_1 <- list(1:15)
list_2 <- list(16:30)
list_3 <- list(31:45)
short_lists[[1]] <- list_1
short_lists[[2]] <- list_2
short_lists[[3]] <- list_3
str(short_lists)
List of 3
 $ :List of 1
  ..$ : int [1:15] 1 2 3 4 5 6 7 8 9 10 ...
 $ :List of 1
  ..$ : int [1:15] 16 17 18 19 20 21 22 23 24 25 ...
 $ :List of 1
  ..$ : int [1:15] 31 32 33 34 35 36 37 38 39 40 ...

And how long I want my tail of a given list to be from list_1, list_2, list_3

how_long <- 
c(4,2,5,3,6,4,7,5,8,6,9,7,10,8,2,4,6,8,10,12,14,10,9,7,11)

And I expand through nested for loops and try to get the tail of the expanded lists, but just get the expanded lists.

for (i in 1:length(how_long)) {
for (j in 1:length(short_lists)) { 
tail_temp[[j]][i] <- tail(short_lists2[[j]], n = how_long[i])
}
}

And this yields:

str(tail_temp)
List of 3
 $ :List of 25
  ..$ : int [1:15] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:15] 1 2 3 4 5 6 7 8 9 10 ...
  ..$ : int [1:15] 1 2 3 4 5 6 7 8 9 10 ...
  [snip]
  ..$ : int [1:15] 1 2 3 4 5 6 7 8 9 10 ...
 $ :List of 25
  ..$ : int [1:15] 16 17 18 19 20 21 22 23 24 25 ...
  ..$ : int [1:15] 16 17 18 19 20 21 22 23 24 25 ...
  ..$ : int [1:15] 16 17 18 19 20 21 22 23 24 25 ...
  [snip]
  ..$ : int [1:15] 16 17 18 19 20 21 22 23 24 25 ...
 $ :List of 25
  ..$ : int [1:15] 31 32 33 34 35 36 37 38 39 40 ...
  ..$ : int [1:15] 31 32 33 34 35 36 37 38 39 40 ...
  ..$ : int [1:15] 31 32 33 34 35 36 37 38 39 40 ...
  [snip]
  ..$ : int [1:15] 31 32 33 34 35 36 37 38 39 40 ...

And I'm happy the j's were expanded, but I never get to the tail call and what I'm seeking:

str(tail_temp)
List of 3
 $ :List of 25
  ..$ : int [1:4] 12 13 14 15
  ..$ : int [1:2] 14 15
  ..$ : int [1:5] 11 12 13 14 15
[snip]

so what simple thing am I missing. Any help appreciated. Thanks.


Solution

  • Very close indeed.

    I prefer vectors in lists over R.
    If you're familiar with python, vectors behave like 'lists' in python.
    Where as lists in R behave like dictionaries.

    Therefore, you just needed to unlist first (into a vector),
    to then assign to an item of a list, hence it should be assigned to:
    tail_temp[[i]][[j]] instead of tail_temp[i][[j]]

    list_1 <- list(1:15)
    list_2 <- list(16:30)
    list_3 <- list(31:45)
    short_lists = list()
    short_lists[[1]] <- list_1
    short_lists[[2]] <- list_2
    short_lists[[3]] <- list_3
    
    how_long <- c(4,2,5,3,6,4,7,5,8,6,9,7,10,
                  8,2,4,6,8,10,12,14,10,9,7,11)
    
    tail_temp = list()
    
    for (i in 1:length(short_lists)){
        tail_temp[[i]] = list()
        for (j in 1:length(how_long)){
           tail_temp[[i]][[j]] <- tail(unlist(short_lists[[i]]), n = how_long[j])
        }
    }
    

    Output

    [[1]]
    [[1]][[1]]
    [1] 12 13 14 15
    
    [[1]][[2]]
    [1] 14 15
    
    [[1]][[3]]
    [1] 11 12 13 14 15
    …
    [[3]][[23]]
    [1] 37 38 39 40 41 42 43 44 45
    
    [[3]][[24]]
    [1] 39 40 41 42 43 44 45
    
    [[3]][[25]]
     [1] 35 36 37 38 39 40 41 42 43 44 45