rnorm()Error in rnorm(): argument "n" is missing, with no default
Introduction to R and RStudio
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 by position, by name, or as a mix of both. It is common to provide the first one or two arguments by position, especially for commonly used functions like t.test() or plot(). For less-commonly used functions or for less-commonly used arguments within functions, you should name arguments to help whoever reads your code.
Consider the rnorm() function. The help page tells us its arguments are
rnorm(n, mean = 0, sd = 1)
This function has three arguments. mean and sd have default values of 0 and 1, respectively, but n has no default value. Any argument without a default value must be provided. Try using rnorm() without any arguments:
We must provide at least n:
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:
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
It is also possible to give all the arguments by position or to name all the arguments.
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).
The value assigned to a function argument can be another data object, or it can be the result of evaluating a sub-expression.
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?
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”. That is, add another argument to mean(): mean(x, ...)