1

I'm wondering if it's possible to apply purrr::pmap (purrr::map2?) combined with purrr::map_depth.

Let's say I have two lists with exact same structure with multiple nested lists, defined as:

  l1 <- list(
    list(0.0132),
    list(
      list(0.033, 0.066)
    ),
    list(
      list(
        list(0.165, 0.11, 0.06),
        list(0.165, 0.22, 0.12)
      )
    )
  )
  
  l2 <- list(
    list(0.6),
    list(
      list(0.75, 0.1)
    ),
    list(
      list(
        list(0.65, 0.71, 0.16),
        list(0.25, 0.98, 0.22)
      )
    )
  )

Is it possible to combine purrr's functions, (pmap, map_depth, vec_depth, etc) to apply some function but taking into account different depths?

Let's suppose I want to get the product l1*l2of each element between lists (e.g. 0.0132*0.6, 0.033*0.75, ...). Using same principle as I'd apply with map2, I have tried passing 3 arguments to pmap, l1, l2, vdepth, but I'm not able to get desired results.

vdepth <- map(l1, vec_depth)

pmap(list(l1, l2, vdepth), function(a, b, d) map_depth(c(a, b), d - 1, ~ FUN))

Final result should have same structure as any of the lists used as inputs.

PS: Not very fan of unlist and trying to stick to purrr's functions...

halfer
  • 19,824
  • 17
  • 99
  • 186
filemonPi
  • 65
  • 5
  • 3
    I note you said you're not a fan of `unlist()` but you can do this elegantly with `unlist()` and `relist()`: `relist(unlist(l1) * unlist(l2), l1)`. What's not to be a fan of?? A `purrr` solution is likely to be considerably more convoluted. – Ritchie Sacramento Feb 20 '23 at 23:45
  • Yes, I'd like to know if it is possible to make use of `purrr`'s tools, but your solution it's completely valid and much more simple, thank you. – filemonPi Feb 21 '23 at 09:33

0 Answers0