Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Composition in R (and high level functions)

Tags:

r

haskell

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 information on high level functional programming in R. I found the Functions "Reduce", "Map", "Filter"..., are there more? Any pointers?

like image 728
mrsteve Avatar asked Feb 07 '11 07:02

mrsteve


People also ask

What are the components of R functions?

To understand functions in R you need to internalise two important ideas: Functions can be broken down into three components: arguments, body, and environment.

Is composition and function same?

In Maths, the composition of a function is an operation where two functions say f and g generate a new function say h in such a way that h(x) = g(f(x)). It means here function g is applied to the function of x. So, basically, a function is applied to the result of another function.


1 Answers

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.)

like image 70
flodel Avatar answered Sep 24 '22 12:09

flodel