Web EndMemo

User Defined Functions


function(arg1,arg2 ...) {...} can be used to define your own R functions. For example, let's define a function which converts Fahrenheit to Celsius:

>ftoc <- function(f){
+ c <- (f - 32 ) * 5 / 9
+ c
+ }
>ftoc(0)
[1] -17.77778

If a function has multiple parameters, and they are given in "name=value" format, they can be in any order.
>x <- func(arg1,arg2,arg3,arg4)
>x <- func(arg1,arg2,arg4=TRUE,arg3=5)
>x <- func(arg3=5,arg4=TRUE,arg1=4,arg2=2)

If a function has default parameter values during the definition, the parameters having default values can be omitted during call:
> x <- func(arg1, arg2=3) { ... }
> Y <- x(4,6)
> y <- x(4)