Call existing R code through functions
This article is originally published at https://www.quantargo.com/blog
When you write code, functions are your best friends. They can make hard things very easy or provide new functionality in a nice way. Through functions you gain access to all the powerful features R has to offer.
 Call functions with function names and round brackets
 Use basic mathematical functions on vectors
 Customize functions through parameters
 Create number sequences using
seq()
 Create random numbers using
runif()
 Sample vectors using
sample()
abs(___) sqrt(___) seq(___) runif(___)
Introduction to functions
Functions in any programming language can be described as predefined, reusable code intended to accomplish a specific task. Functions in R can be used by using their name and round brackets right after the that. Inside the brackets, we can specify parameters for the function. One function we have already used extensively is the concatenate function c()
.
A simple function for example is abs()
which is used to get the absolute value of a number. In the following example, the function is given 3
as input and returns the result 3
:
abs(3)
[1] 3
Exercise: Use the sqrt() function
Use the sqrt()
function to get the squareroot of 8.
Customizing functions through parameters
Functions take parameters, that customize them for the given task. For example, the runif()
function generates uniformly distributed values, which means that all outcomes have the same probability. By default, it takes the following parameters:
runif(n, min = 0, max = 1)
The first parameter n
is the number of values we want to generate. This is a mandatory parameter, that we need to define, in order for the function to work.
On the other hand, we can see that some of the parameters have default values defined by the equals sign =
. This means that if we don’t explicitly specify these parameter in the brackets, the function will take the default ones. Let’s take a look at an example:
runif(n = 5)
[1] 0.84923212 0.05486089 0.16629374 0.48296289 0.78885247
The output is a numeric vector of 5 numbers. Each of them is between 0 and 1, since we did not change the default setting. If we changed the parameters min
and max
as well, we could further customize the output:
runif(n = 5, min = 8, max = 9)
[1] 8.634561 8.416060 8.570673 8.965559 8.765042
It is also possible to leave out the name of the parameters and simply type in the input values like this:
runif(5, 8, 9)
[1] 8.166321 8.381304 8.826278 8.771048 8.137975
However, in this case we must be cautious about the order of inputs, since each function has a default order for the parameters. If we don’t explicitly name the parameters we are setting, R will assume, that we set them in the predefined order.
Exercise: Use the sample() function
The sample()
function takes a vector and returns a random sample from it. The first two of its parameters are:

x
, which defines the vector 
size
, which defines the number of elements we want to include in the random sample
Use the sample()
function and sample 5 random values from the full
variable.
Exercise: Use the seq() function
The seq()
function creates a sequence of whole numbers. The first three of its parameters are: from
, to
and by
.

from
defines the start of the sequence 
to
defines the end of the sequence 
by
sets the steps between the single values
Use the seq()
function and create a sequence of numbers from 2 to 10 but only include every second value. Thus, the output should be: 2
, 4
, 6
, 8
, 10
.
Call existing R code through functions is an excerpt from the course Introduction to R, which is available for free on quantargo.com
Thanks for visiting rcraft.org
This article is originally published at https://www.quantargo.com/blog
Please visit source website for post related comments.