What is the practical use of the identity function in R?

Andrie picture Andrie · Aug 18, 2011 · Viewed 7.4k times · Source

Base R defines an identity function, a trivial identity function returning its argument (quoting from ?identity).

It is defined as :

identity <- function (x){x}

Why would such a trivial function ever be useful? Why would it be included in base R?

Answer

Ingo picture Ingo · Aug 18, 2011

Don't know about R, but in a functional language one often passes functions as arguments to other functions. In such cases, the constant function (which returns the same value for any argument) and the identity function play a similar role as 0 and 1 in multiplication, so to speak.