Apply list-specific cutoff value to individual vectors in nested list

Viewed 113

I have a nested list, have_list. At the center is a list with four vectors of integers, a, b, c, d.

For a, b, c, d, each has a unique cutoff value. I would like to find the first positions when the integer is greater than the relevant cutoff value.

I can do this if a-d had the same cutoff by:

rapply(have_list, function(x) which.max(x > cutoff), how = "list")

My specific question is how to use the respective cutoff values for a-d, without for loops, if possible. I can't seem to find anything on the internet or SO, though apologies if I overlooked a previous question.

Sample data

cutoff <- c(a = 5, b = 17, c = 11, d = 7)

set.seed(05062020)
have_list <- list(Outer1 = list(a = sample(1:25, 10),
                                b = sample(1:25, 10),
                                c = sample(1:25, 10),
                                d = sample(1:25, 10)),
                  Outer2 = list(a = sample(1:25, 10),
                                b = sample(1:25, 10),
                                c = sample(1:25, 10),
                                d = sample(1:25, 10)))

Desired data

want_list <- list(Outer1 = list(a = 2, b = 2, c = 1, d = 1),
                  Outer2 = list(a = 1, b = 4, c = 4, d = 1))
4 Answers

You can use lapply to move through the "Outer" lists, and Map to compare each inner list to the corresponding cutoff:

lapply(have_list, \(x) {Map(\(lst, cuts) {
  return(which(lst > cuts)[1])
}, x, cutoff)})

Here is the str of this output:

List of 2
 $ Outer1:List of 4
  ..$ a: int 2
  ..$ b: int 2
  ..$ c: int 1
  ..$ d: int 1
 $ Outer2:List of 4
  ..$ a: int 1
  ..$ b: int 4
  ..$ c: int 4
  ..$ d: int 1

How about something like this:

lapply(have_list, function(x) {
  as.list(sapply(names(x), function(y) {
    min(which(x[[y]]>cutoff[[y]]))
  }))
})

Output:

$Outer1
$Outer1$a
[1] 2

$Outer1$b
[1] 2

$Outer1$c
[1] 1

$Outer1$d
[1] 1


$Outer2
$Outer2$a
[1] 1

$Outer2$b
[1] 4

$Outer2$c
[1] 4

$Outer2$d
[1] 1

Another possible solution, based on purrr::map and purrr::map2:

library(purrr)

map(have_list, ~ map2(.x, cutoff, ~ which.max(.x > .y)))

#> $Outer1
#> $Outer1$a
#> [1] 2
#> 
#> $Outer1$b
#> [1] 2
#> 
#> $Outer1$c
#> [1] 1
#> 
#> $Outer1$d
#> [1] 1
#> 
#> 
#> $Outer2
#> $Outer2$a
#> [1] 1
#> 
#> $Outer2$b
#> [1] 4
#> 
#> $Outer2$c
#> [1] 4
#> 
#> $Outer2$d
#> [1] 1

For abritrarily nested lists, you can also use rrapply() in the rrapply-package, which can access the name of the list element under evaluation:

library(rrapply)

want_list <- rrapply(have_list, f = \(x, .xname) which.max(x > cutoff[.xname]))

str(want_list)
#> List of 2
#>  $ Outer1:List of 4
#>   ..$ a: int 2
#>   ..$ b: int 2
#>   ..$ c: int 1
#>   ..$ d: int 1
#>  $ Outer2:List of 4
#>   ..$ a: int 1
#>   ..$ b: int 4
#>   ..$ c: int 4
#>   ..$ d: int 1
Related