Function Composition in R (and high level functions)

后端 未结 3 950
执笔经年
执笔经年 2021-02-19 01:28

Is there something like a function composition in R?

I think in haskell it\'s somthing like \"(.)\" and in agda it\'s the ring operator.

Also, I find litte infor

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-19 02:08

    The functional package has a Compose functional which generalizes to any number of functions:

    set.seed(123)
    x <- matrix(runif(100), 10, 10)
    mean(rowSums(scale(x)))
    # [1] 5.486063e-18
    
    library(functional)
    Compose(scale, rowSums, mean)(x)
    # [1] 5.486063e-18
    

    (Note that the functions are applied from left to right.)

提交回复
热议问题