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] -1.621328669 -0.289892904  0.005805381 -0.833671181 -0.210553670

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]  6.316367  9.110389 10.485864 12.489924 11.143730

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

  1. Look up the help page for read.table(). What is the difference in the argument defaults for read.csv() and read.csv2()? Why is there this difference?

  2. Look up the help page for mean(). How does it handle missing values (NA) by default? Adjust the arguments of mean() below so that it produces the result “3”.

x <- c(1, 5, NA)
mean(x)