This Unit aims to Write user-defined functions
A new function is defined with the following synthax:
function_name <- function(x) { ... }
where:
function_name
is the name of the functionfunction()
function callx
is the argument{...}
space for typing the functionFor instance, let’s define a new function that calculate the coeffient of variation or “CV”. This is determined as:
This algorithm can be implemented in a user-defined function in the following way:
CV = function(x) {sd(x)/mean(x)}
Now, having defined the following vector:
A <- c(5,4,6,4,5,4,6,3)
Let’apply the function CV
:
CV(A)
## [1] 0.2293319