Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 625
Andrie Avatar asked Aug 18 '11 14:08

Andrie


People also ask

What is the use of identity function?

The identity function is a function which returns the same value, which was used as its argument. It is also called an identity relation or identity map or identity transformation. If f is a function, then identity relation for argument x is represented as f(x) = x, for all values of x.

What is r identity?

identity() function in R Language is used to print the value of the object which is passed to it as argument.

What is an identity function programming?

An identity function, in the context of the code in your question, is simply a function that returns the same argument passed to it : In math you can denote it as: f(x) = x.

Why is it called an identity function?

It is called an identity function because the image of an element in the domain is identical to the output in the range. Thus, an identity function maps each real number to itself. The output of an identity function is the same as its input.


2 Answers

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.

like image 160
Ingo Avatar answered Sep 28 '22 20:09

Ingo


I use it from time to time with the apply function of commands.

For instance, you could write t() as:

dat <- data.frame(x=runif(10),y=runif(10)) apply(dat,1,identity)         [,1]      [,2]      [,3]      [,4]      [,5]      [,6]       [,7] x 0.1048485 0.7213284 0.9033974 0.4699182 0.4416660 0.1052732 0.06000952 y 0.7225307 0.2683224 0.7292261 0.5131646 0.4514837 0.3788556 0.46668331        [,8]      [,9]      [,10] x 0.2457748 0.3833299 0.86113771 y 0.9643703 0.3890342 0.01700427 
like image 36
Ari B. Friedman Avatar answered Sep 28 '22 21:09

Ari B. Friedman