Easily finding and replacing every match in a nested list

前端 未结 1 690
一向
一向 2021-01-12 16:20

Take this object as an example:

expr <- substitute(mean(exp(sqrt(.)), .))

It is a nested list. I want to find every element that matches

相关标签:
1条回答
  • 2021-01-12 17:03

    Using a recursive function:

    convert.call <- function(x, replacement) {
      if (is.call(x)) as.call(lapply(x, convert.call, replacement=replacement)) else
        if (identical(x, quote(.))) as.name(replacement) else
          x
    }
    
    convert.call(expr, "x")
    # mean(exp(sqrt(x)), x)
    
    0 讨论(0)
提交回复
热议问题