Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
R (programming language)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Structure of a function === R is able to create [[Function (computer programming)|functions]] to add new functionality for reuse.<ref>{{cite web|url=http://www.statmethods.net/management/userfunctions.html|title=Quick-R: User-Defined Functions|first=Robert|last=Kabacoff|year=2012|access-date=2018-09-28|website=statmethods.net}}</ref> [[Object (computer science)|Objects]] created within the body of the function (which are enclosed by curly brackets) remain [[Local variable|only accessible]] from within the function, and any [[data type]] may be returned. In R, almost all functions and all [[user-defined function]]s are [[closure (computer programming)|closures]].<ref>{{cite web|url=http://adv-r.had.co.nz/Functional-programming.html#closures|title=Advanced R - Functional programming - Closures|website=adv-r.had.co.nz|first=Hadley|last=Wickham}}</ref> Example of creating a function to perform some arithmetic calculation: <syntaxhighlight lang="r"># The input parameters are x and y. # The function, being named f, returns a linear combination of x and y. f <- function(x, y) { z <- 3 * x + 4 * y # An explicit return() statement is optional, could be replaced with simply `z`. return(z) } # Alternatively, the last statement executed is implicitly returned. f <- function(x, y) 3 * x + 4 * y</syntaxhighlight> Usage output: <syntaxhighlight lang="rout"> > f(1, 2) # 3 * 1 + 4 * 2 = 3 + 8 [1] 11 > f(c(1, 2, 3), c(5, 3, 4)) # Element-wise calculation [1] 23 18 25 > f(1:3, 4) # Equivalent to f(c(1, 2, 3), c(4, 4, 4)) [1] 19 22 25 </syntaxhighlight> It is possible to define functions to be used as [[Infix notation|infix operators]] with the special syntax <code>`%name%`</code> where "name" is the function variable name: <syntaxhighlight lang="rout"> > `%sumx2y2%` <- function(e1, e2) {e1 ^ 2 + e2 ^ 2} > 1:3 %sumx2y2% -(1:3) [1] 2 8 18 </syntaxhighlight> Since version 4.1.0 functions can be written in a short notation, which is useful for passing anonymous functions to higher-order functions:<ref>{{cite web|url=https://cran.r-project.org/doc/manuals/r-release/NEWS.html#:~:text=R%20now%20provides%20a%20shorthand%20notation%20for%20creating%20functions|title=NEWS|website=r-project.org}}</ref> <syntaxhighlight lang="rout"> > sapply(1:5, \(i) i^2) # here \(i) is the same as function(i) [1] 1 4 9 16 25 </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)