round(x = 3.14159, digits = 2)[1] 3.14
On the last page, you already changed the inputs to round() and saw the output change. Those inputs have a name: arguments. Arguments are simply the inputs you give to a function, written inside its parentheses.
function_name(arguments)
Most R functions accept more than one argument, and each argument has its own name. You specify argument values by writing a comma-separated list of name = value pairs between the parentheses of the function call.
Do: run the two lines of code.
See: both lines produce the same result.
Explain: if you supply argument values in the order the function expects them, you don’t have to write out the argument names. Both round(x = 3.14159, digits = 2) and round(3.14159, 2) tell R exactly the same thing — the second version is just a shortcut.
The mean() function accepts an argument called trim, which excludes a proportion of the most extreme values before averaging. Predict what each line below will print.
mean(x) averages all five values, including the outlier 100, which pulls the average up substantially. mean(x, trim = 0.2) first removes the most extreme 20% of values on each end before averaging, giving a result that’s much closer to the “typical” value in the data.
You don’t always need to write out an argument’s name, but if you skip the name, R will assume you’re supplying the arguments in the order the function expects. Mixing these two styles is common and perfectly fine, as long as you keep the ordering rules straight:
We’ll come back to exactly how you find out a function’s expected argument order and default values once we cover documentation later in this module. For now, it’s enough to recognize that arguments have names, and that those names matter.
round(3.14159, 2), which part is the function name, and which part(s) are the arguments?round(3.14159, 2) using explicit argument names.