How to Map() a function recursively (through nested lists) in R?

穿精又带淫゛_ 提交于 2019-12-08 13:21:01

问题


Disclaimer: this is not a duplicate of this question: How to combine rapply() and mapply(), or how to use mapply/Map recursively? On top of that question, I am asking how to incorporate extra arguments of functions into the recursion.

So I have lists:

A = list(list(list(c(1,2,3), c(2,3,4)),list(c(1,2,3),c(2,3,4))), list(c(4,3,2), c(3,2,1)))
B = list(list(list(c(1,2,3), c(2,3,4)),list(c(1,2,3),c(2,3,4))), list(c(4,3,2), c(3,2,1)))

And I need to apply different functions to it recursively that preserves the list structure. Presumably, a recursive function that does this goes like this, when the function is mean():

recursive = function(x, y){
  if(is.null(y)){
    if(is.atomic(x)){
      x*x
    } else{
      Map(recursive, x)
    }
  } else{
    if(is.atomic(y) && is.atomic(x)){
      x*y
    } else{
      Map(recursive, x, y)
    }
  }
}

So the desired result would be:

recursive(A,B)

I was wondering how could I generalize this recursion to any functions beyond just the hard-coded function(x,y) x*y here so that I could change functions conveniently? In that case, it would start with:

recursive = function(somefunction, x, y){
....
}

where

somefunction = function(x,y) x*y #or any other function taking x and y as inputs

Could anyone kindly show me a way out? Thank you so much.


回答1:


You can use

recursive  <- function(fun, x, y) {
    if(is.atomic(x) && is.atomic(y)) {
        match.fun(fun)(x, y) 
    } else  {
        Map(recursive, x, y, MoreArgs=list(fun=fun))
    }
}

The reason is that Map calls mapply and mapply has a MoreArgs= parameter where you can specify other parameters you want to pass to the calling function that you do not want to iterate over.



来源:https://stackoverflow.com/questions/37996470/how-to-map-a-function-recursively-through-nested-lists-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!