Название | The Big R-Book |
---|---|
Автор произведения | Philippe J. S. De Brouwer |
Жанр | Математика |
Серия | |
Издательство | Математика |
Год выпуска | 0 |
isbn | 9781119632771 |
apply
-family functions (see Chapter 4.3.5 “Arrays” on page 50) or their tidyverse equivalents: the map
-family of functions of the package purrr
. An example for the apply-family can be found in Chapter 22.2.6 on page 513 and one for the mapfamily is in Chapter 19.3 on page 459.
Further information about optimising code for speed and more elegant and robust timing of code can be found in Chapter 40 “The Need for Speed” on page 997.
4.6 Functions
More than in any other programming language, functions play a prominent role in R. Part of the reason is the implementation of the dispatcher function based object model with the S3 objects—see Chapter 6 “The Implementation of OO” on page 117.
functions
The user is both able to use the built-in functions and/or define his own bespoke functions.
4.6.1 Built-in Functions
Right after starting, R some functions are available. We call these the “built-in functions.” Some examples are:
demo(): shows some of the capabilities of R
demo()
q(): quits R
q()
data(): shows the datasets available
data()
help(): shows help
help()
ls(): shows variables
ls()
c(): creates a vector
c()
seq(): creates a sequence
seq()
mean(): calculates the mean
mean()
max(): returns the maximum
max()
sum(): returns the sum
sum()
paste(): concatenates vector elements
paste()
4.6.2 Help with Functions
If you do not remember exactly what parameters a certain function needs or what type of variable the output will be, then there are multiple ways to get support in R.
apropos()
Help with functions
help(c) # shows help help with the function c ?c # same result apropos(“cov”) # fuzzy search for functions
R has many “packages” that act as a library of functions that can be loaded with one command. For more information refer to Chapter 4.7 “Packages” on page 96.
4.6.3 User-defined Functions
function()
The true flexibility comes from being able to define our own functions. To create a function in R, we will use the function-generator called “function.”
function – create
Function use for function()
In R a user defined function (UDF) is created via the function function()
.
function_name <- function(arg_1, arg_2, …) { function_body return_value }
Example: A bespoke function
# c_surface # Calculates the surface of a circle # Arguments: # radius -- numeric, the radius of the circle # Returns # the surface of the cicle c_surface <- function(radius) { x <- radius ∧ 2 * pi return (x) } c_surface(2) + 2 ## [1] 14.56637
Note that it is not necessary to explicitly “return” something. A function will automatically return the last value that is send to the standard output. So, the following fragment would do exactly the same:
# c_surface # Calculates the surface of a circle # Arguments: # radius -- numeric, the radius of the circle # Returns # the surface of the cicle c_surface <- function(radius) { radius ∧ 2 * pi } # Test the function: c_surface(2) + 2 ## [1] 14.56637
4.6.4 Changing Functions
Usually,we will keep functions in a separate file that is then loaded in our code with the command source()
. Editing a function is then done by changing this file and reloading it – and hence overwriting the existing function content.
Most probably you will work in a modern environment such as the IDE RStudio,which makes editing a text-file with code and running that code a breeze. However, there might be cases where one has only terminal access to R. In that case, the following functions might come in handy.
edit()
fix()
# Edit the function with vi: fix(c_surface) # Or us edit: c_surface <- edit()
The edit()
function uses the vi
editor when using the CLI on Linux. This editor is not so popular any more and you might not immediately know how to close it. To get out of it: press [esc]
, then type :q
and press [enter]
.
vi
4.6.5 Creating Function with Default