1.15 Functions

We have already seen a few functions, like log(x), sqrt(x), and is.numeric(x). Functions typically have one or more arguments, which are the names you put in the parentheses after the function name (although there are some functions that require no arguments, like Sys.time() which returns the current time). To learn more about a function, you can use the args() function to see what arguments the function allows, and you can type ? followed by the name of the function to see a detailed help file that explains the nature of each argument, as well as additional details to help you understand what the function is doing, references, and example code.

For example, let’s look at the function rnorm() that allows you to generate normally distributed random numbers.

args(rnorm)
## function (n, mean = 0, sd = 1) 
## NULL

The function rnorm() has three arguments: n, mean, and sd. Arguments followed by an = have a default value and are not required to be specified. Therefore, when calling rnorm(), you must specify a value for n and, for each of mean and sd, you can optionally specify a value or leave it out of the function call and accept the default value.

But what does this function do, and what do each of the arguments represent? Use ? followed by a function name to view its help file.

?rnorm

The help file specifies what each argument represents. rnorm() generates n random numbers from a normal distribution with the mean and sd you specify. For example, to generate 5 random values from a normal distribution with a mean of 100 and a standard deviation of 14, use the following line of code.

rnorm(n = 5, mean = 100, sd = 14)
## [1]  97.13  93.80 112.47 130.48 122.67

If you re-run the code above, you will get different numbers, because it is random (technically, pseudo-random)! To have reproducible results even in the presence of (pseudo) randomness, use set.seed(). For example, if you run the following code, you should get the same results as shown here (assuming nothing has changed between how R generates random numbers between the version used here (R version 4.3.2 (2023-10-31 ucrt)) and whatever version you are running).

set.seed(294783)
rnorm(n = 5, mean = 100, sd = 14)
## [1]  97.72  95.54 115.51 115.27  77.20

You can enter any number inside set.seed(). You will get different results using different seeds, but if you use the same seed every time you will get the same result every time.

set.seed(294783)
rnorm(n = 5, mean = 100, sd = 14)
## [1]  97.72  95.54 115.51 115.27  77.20
set.seed(874)
rnorm(n = 5, mean = 100, sd = 14)
## [1] 107.37  98.65  86.13  96.19 106.75

NOTE: When you use a function, you can name the arguments (as we did above) or not, but if you do not name them then R assumes they appear in the same order as shown by the args function, so be careful!

set.seed(874)
rnorm(5, 100, 14)
## [1] 107.37  98.65  86.13  96.19 106.75

NOTE: If you leave the optional arguments out entirely, then R will use their default values. In this case, mean 0 and standard deviation 1. The following 3 calls to rnorm() produce the same results.

set.seed(9472309)
rnorm(5)
## [1] -1.1035 -0.5612  1.8617 -1.6827  2.0054
set.seed(9472309)
rnorm(5, mean = 0, sd = 1)
## [1] -1.1035 -0.5612  1.8617 -1.6827  2.0054
set.seed(9472309)
rnorm(5, 0, 1)
## [1] -1.1035 -0.5612  1.8617 -1.6827  2.0054