## 3.2 Functions in R

• In all research fields there are few statistical (or otherwise) calculations which are used frequently by the users, for example calculation of returns in finance research.

• R provides the facility of creating specific functions to evaluate a set of arguments and return an output value which are stored as R objects.

• The functions in R are created by the keyword $$\mathtt{function}$$ which takes the following syntax.

$\mathtt{function(arguments)}body$

• The arguments are the values/defaults/variables which are used in the body of the function to evaluate an expression.

• The body of the function is enclosed in curly braces. The function is assigned to a named object and called by passing arguments to the object.

• The following example illustrates by creating a function to calculate mean for all the columns in the data set $$\mathtt{data\_stocks}$$

# the following function takes 2 arguments, x a data frame, dates to indicate
# if there are dates in the first column
cal_mean = function(x, dates = TRUE) {
num_cols = ncol(x)  #calculate the number of columns
# num_cols=ifelse(dates==TRUE,num_cals-1,num_cals) lets use a list and a
# loop to refresh our concepts
m_stocks = list()  #creating an empty list

# use for loop assign the starting value based on the dates column,we skip
# dates column if they are present (dates are basically row names to more
# generalised version will be to check for row names)
l = ifelse(dates == TRUE, 2, 1)
j = 1  #starting point in the list m_stocks
for (i in l:num_cols) {
m_stocks[[j]] = mean(x[, i])
j = j + 1
}
names(m_stocks) = colnames(x[, l:num_cols])
return(m_stocks)
}
# lets call the function cal_mean (output not shown)
cal_mean(data_stocks, TRUE)
$MSFT  26.91177$IBM
 122.3303

$AAPL  207.7967$MCD
 58.95141

$PG  61.32512$GOOG
 469.9453
# lets call the function with no dates column
cal_mean(data_stocks[, 2:ncol(data_stocks)], FALSE)
$MSFT  26.91177$IBM
 122.3303

$AAPL  207.7967$MCD
 58.95141

$PG  61.32512$GOOG
 469.9453