9 Using Functions
As described in the last section, most functions take input in the form of arguments and return output in the form of a data object (the return “value”).
The arguments may be given in order (positionally), by name, or as a mix of both. Common style is to fill in the first argument positionally, and to give other arguments by name.
Consider the rnorm()
function. The help page tells us its arguments are
rnorm(n, mean = 0, sd = 1)
This function has three arguments, two of which also have default values.
If we use this function with one argument
rnorm(5)
[1] -0.4160736 0.7847615 -0.7930703 1.1357843 -1.2618035
the “5” in our code is understood to be the first argument, n
. Rather than assigning n
a value by its position in our code, we could equally have specified it by name
rnorm(n = 5)
Now suppose we want our random numbers to come from a distribution with a mean of 10 and a standard deviation of 2. The clearest style would be to write
rnorm(5, mean = 10, sd = 2)
[1] 7.923415 13.039880 11.963566 8.371098 8.702121
It is also possible to give all the arguments by position or to name all the arguments.
rnorm(5, 10, 2) # by position
rnorm(n = 5, mean = 10, sd = 2) # by name
If we are using names, the arguments do not have to be in any order (although good style usually preserves the order anyway, for readability).
rnorm(sd = 2, n = 5, mean = 10)
The value assigned to a function argument can be another data object, or it can be the result of evaluating a sub-expression.
x <- 2
rnorm(5, mean = 5*x, sd = x)
9.1 Exercises
Look up the help page for
read.table()
. What is the difference in the argument defaults forread.csv()
andread.csv2()
? Why is there this difference?Look up the help page for
mean()
. How does it handle missing values (NA
) by default? Adjust the arguments ofmean()
below so that it produces the result “3”.
x <- c(1, 5, NA)
mean(x)