Search code examples
rlistnestedsubsetrun-length-encoding

Conditional Subsetting of a Nested List


I have a nested list where ach sub list has two lists. A simplified output of this list is below:

nested.list <- list(`1` = structure(list(lengths = c(325L, 18L, 1L, 7L, 1L, 
10L, 1L, 35L, 1L, 1L, 152L, 1L, 1L, 37L, 1L, 33L, 1L, 15L, 2L, 
1L, 47L, 1L, 29L, 107L, 35L, 3L, 6L, 12L, 16L), values = c(1, 
2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 129, 
1, 2, 1, 2, 1, 2, 1)), class = "rle"), `2` = structure(list(lengths = c(19L, 
99L, 1L, 16L, 2L, 24L, 1L, 12L, 1L, 23L, 1L, 2L, 1L, 51L, 1L, 
1L, 2L, 34L, 1L, 15L, 1L, 20L, 1L, 8L, 1L, 18L, 28L, 7L, 105L, 
3L, 23L, 1L, 13L, 1L, 17L, 1L, 31L, 1L, 17L, 6L, 5L, 2L, 18L, 
1L, 20L, 2L, 38L, 21L, 9L, 2L, 89L, 1L, 1L, 2L, 26L, 4L, 1L, 
1L, 1L, 3L, 4L, 6L, 1L, 7L, 1L, 24L), values = c(1, 2, 1, 2, 
1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
2, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
2, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2)), class = "rle"), 
    `3` = structure(list(lengths = c(6L, 3L, 14L, 1L, 1L, 1L, 
    1L, 1L, 9L, 2L, 29L, 3L, 11L, 1L, 19L, 1L, 20L, 1L, 2L, 1L, 
    22L, 11L, 2L, 1L, 7L, 2L, 17L, 30L, 3L, 1L, 1L, 1L, 8L, 4L, 
    5L, 1L, 23L, 1L, 11L, 4L, 2L, 1L, 29L, 2L, 10L, 1L, 17L, 
    6L, 12L, 1L, 22L, 22L, 8L, 3L, 2L, 13L, 1L, 1L, 1L, 83L, 
    1L, 24L, 1L, 24L, 1L, 7L, 6L, 4L, 1L, 2L, 1L, 37L, 1L, 32L, 
    33L, 5L, 1L, 29L, 6L, 7L, 16L, 2L, 16L, 1L, 1L, 1L, 3L, 1L, 
    18L, 1L, 9L, 4L, 3L, 1L, 21L, 14L, 12L, 1L, 33L), values = c(2, 
    1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
    2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 
    1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 1, 129, 1, 2, 1, 2, 
    1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 3, 1, 2, 
    1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 
    2, 1, 2)), class = "rle"))

This was generated by lapply(another.list, function(x) rle(x)) in case that is of importance.

Anyway, I know how to extract individual elements by doing something like nested.list[[1]]["lengths"]. However I am looking to conditionally subset each pair of lists so that I have all lengths >= 6 and values == 2.

I have looked for previous answers and found examples such as lapply(list,[, list$value == 2), but they do not extend to my case and I haven't been able to get them to.

I imagine there must be a simple way of doing this that I am missing.


Solution

  • Here is a base R version which returns data.frames.

    lapply(nested.list,function(x){
      y <- data.frame(lengths = x$length, values = x$values);
      y[y$lengths >= 6 & y$values ==2,]})
    #$`1`
    #   lengths values
    #2       18      2
    #4        7      2
    #6       10      2
    #...
    #
    #$`2`
    #   lengths values
    #2       99      2
    #4       16      2
    #6       24      2
    #...
    #
    #$`3`
    #   lengths values
    #1        6      2
    #3       14      2
    #9        9      2
    #...