Optional arguments
Let's look at some of the round()
function's help documentation. It simply rounds a numeric vector off to a specified number of decimal places.
round(x, digits = 0)
The first argument, x
is required. Without it, the function will not work!
The argument digits
is known as an optional argument. Optional arguments are ones that don't have to be set by the user, either because they are given a default value, or because the function can infer them from the other data you have given it. Even though they don't have to be set, they often provide extra flexibility. Here, digits
specifies the number of decimal places to round to.
Explore the round()
function in the exercise!
This is a part of the course
“Intermediate R for Finance”
Exercise instructions
- Use
round()
on5.4
. - Use
round()
on5.4
, specifydigits = 1
. - A vector
numbers
has been created for you. - Use
round()
onnumbers
and specifydigits = 3
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Round 5.4
___
# Round 5.4 with 1 decimal place
___
numbers <- c(.002623, pi, 812.33345)
# Round numbers to 3 decimal places
___