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?
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.
identity() function in R Language is used to print the value of the object which is passed to it as argument.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With