User Defined Functions

Aim of the Unit

This Unit aims to Write user-defined functions

Define a new function

A new function is defined with the following synthax:

function_name <- function(x) { ... }


For instance, let’s define a new function that calculate the coeffient of variation or “CV”. This is determined as:

CV=standard deviationmean

This algorithm CV=sx¯ 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:

## [1] 0.2293319