-
Book Overview & Buying
-
Table Of Contents
Learning Quantitative Finance with R
By :
In this section, we will provide some examples of built-in functions that already exist in R and also construct a user-defined function for a specific task.
A function is a collection of statements put together to do a specific task.
R has a lot of built-in functions and users can define their own functions.
According to their requirement, in R, the interpreter passes control to the function object along with the arguments required for the accomplishment of the task designated for the function. After completing the task, the function returns the control to the interpreter.
The syntax for defining a function is as follows:
>function_name<-function(arg1, arg2,...){
>+function body
>+}
Here:
Please find here an example of some of the inbuilt functions along with their results when executed:
>print(mean(25:82)) [1] 53.5 >print(sum(41:68)) [1] 1526
Now we will look at how to build the user-defined functions. Here we are trying to find the square of a given sequence.
The name of the function is findingSqrFunc and takes the argument value, which must be an integer:
>findingSqrFunc<-function(value){
>+for(j in 1:value){
>+sqr<-j^2
>+print(sqr)
>+}
>+}
Once the preceding code gets executed, we call the function:
>findingSqrFunc(4)
We get the following output:
[1] 1 [1] 4 [1] 9 [1] 16
Construct a function without an argument:
>Function_test<-function(){
>+ for(i in 1:3){
>+ print(i*5)
>+ }
>+ }
>Function_test()
On executing the preceding function without arguments, the following output gets printed:
[1] 5 [1] 10 [1] 15
The arguments to a function can be supplied in the same sequence as the way it has been defined. Otherwise the arguments have to be given in any order but assigned to their name. Given here are the steps for creating and calling the functions:
>Function_test<-function(a,b,c){
>+ result<-a*b+c
>+ print(result)
>+ }
>Function_test(2,3,4)
[1] 10
>Function_test(c=4,b=3,a=4)
This gives the following output:
[1] 16
In this section, we will discuss different ways of executing R programs.
For running a program in the R workspace, follow these steps:
Please perform the following steps for sourcing the R code:
>print(getwd())
>setwd("D:/Rcode")
>Source('firstprogram.r')
For example, let's say the program firstprogram.r has the following code in it:
a<-5 print(a)
Upon sourcing, it will generate the output 5 at the console.
When you want to tell R to execute a number of lines of code without waiting for instructions, you can use the source function to run the saved script. This is known as sourcing a script.
It's better to write the entire code in Studio Editor and then save it and source the entire script. If you want to print an output in source script then please use the print function to get the desired output. However, in the interactive editor, you do not need to write print. It will give it by default.
In other operating systems, the command for running the program remains the same.
Comments are parts of a program that are ignored by the interpreter while executing the actual program.
Comments are written using #; for example:
#this is comment in my program.