Method chaining with R

前端 未结 4 1450
一生所求
一生所求 2021-02-20 14:19

Is it possible to chain functions in R?

Sample data:

m <- matrix(c(1:10, 11:20), nrow = 10, ncol = 2)

For example, I would like to r

4条回答
  •  失恋的感觉
    2021-02-20 15:10

    In a similar vein to Ben's answer, but allowing arguments:

    `%@%` <- function(x, f) eval.parent(as.call(append(as.list(substitute(f)), list(x), 1)))
    
    x %@% mean %@% sqr # => 6.25
    c(1, 2, NA, 3, 4) %@% mean(na.rm=T) %@% sqr # => 6.25
    m %@% colMeans() %@% sum() # => 21
    

提交回复
热议问题