mean(x = c(1,5,6)) # or short: mean(c(1,5,6))
#> [1] 4
Day 1 - Introduction to Data Analysis with R
Freie Universität Berlin - Theoretical Ecology
March 14, 2025
Functions make multiple operations available under one command.
General structure of a function call: function_name ( argument = value )
function_name ( argument = value )
mean ( x = c(1,5,6) )
But what does the mean function do? What are the arguments that I can use?
What happened here?
na.rm
argument is FALSE
by default.
Set it to TRUE
if you want to calculate the mean despite missing values:
Argument matching can be achieved by position or by name
Argument matching can be achieved by position or by name
Named arguments are (generally) preferred
R basics - Functions
Functions take input in the form of (named) arguments, calculate something and return a result
Functions are called by their name, followed by parentheses:
functionName(argument1 = value, argument2 = value, ...)
Functions from additional packages can be called in two ways:
packageName::functionName()
library(packageName)
then call the function anywhere in the script with functionName()
Call ?functionName
to open the help of a function
Selina Baldauf // Functions in R