## Built-in functions

Examples

x = 5
abs(x)           # x is the argument of the function
## [1] 5
sin(x)
## [1] -0.9589243
exp(x)
## [1] 148.4132
as.character(x)
## [1] "5"
log(x)
## [1] 1.609438
?log             # get help for the log function
help(log)        # same
x = rnorm(100)   # a sample of size 100 from the standard normal distribution
?rnorm           # default values:  mean = 0 and sd = 1
mean(x)
## [1] -0.01117017
sd(x)
## [1] 0.9549603
median(x)
## [1] -0.1007972
max(x)
## [1] 2.21257
min(x)
## [1] -2.16132
sum(x)
## [1] -1.117017

## User-defined functions

General form:

myfunction <- function(arg1, arg2 = some_value, ... ){  # arg2 has a default value!
statements
return(object)
}

Example:

get.square <- function(x) {
squared = x^2
return(squared)
}
get.square(2)
## [1] 4
get.square(1.653525)
## [1] 2.734145
get.square(sqrt(2))
## [1] 2

## Load functions from disk into workspace

getwd()
## [1] "/home/uwe/Desktop/R-course"
file.exists("plot_norm.R")
## [1] TRUE
source("plot_norm.R")
plot_norm(mean = 2, sd = 1)