Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Quantitative Finance with R

You're reading from   Learning Quantitative Finance with R Implement machine learning, time-series analysis, algorithmic trading and more

Arrow left icon
Product type Paperback
Published in Mar 2017
Publisher Packt
ISBN-13 9781786462411
Length 284 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
PRASHANT VATS PRASHANT VATS
Author Profile Icon PRASHANT VATS
PRASHANT VATS
Dr. Param Jeet Dr. Param Jeet
Author Profile Icon Dr. Param Jeet
Dr. Param Jeet
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Introduction to R 2. Statistical Modeling FREE CHAPTER 3. Econometric and Wavelet Analysis 4. Time Series Modeling 5. Algorithmic Trading 6. Trading Using Machine Learning 7. Risk Management 8. Optimization 9. Derivative Pricing

How to write code expressions

In this section, we will discuss how to write various basic expressions which are the core elements of writing a program. Later, we will discuss how to create user-defined functions.

Expressions

R code consists of one or more expressions. An expression is an instruction to perform a particular task.

For example, the addition of two numbers is given by the following expression:

>4+5 

It gives the following output:

[1] 9 

If there is more than one expression in a program, they get executed one by one, in the sequence they appear.

Now we will discuss basic types of expressions.

Constant expression

The simplest form of expression are constant values, which may be character or numeric values.

For example, 100 is a numeric value expression of a constant value.

Hello World is a character form expression of a constant expression.

Arithmetic expression

The R language has standard arithmetic operators and using these, arithmetic expressions can be written.

R has the following arithmetic operators:

Operands

Operators

+

Addition

-

Subtraction

*

Multiplication

/

Division

^

Exponentiation

Using these arithmetic operations, one can generate arithmetic expressions; for example:

4+5 
4-5 
4*5 

R follows the BODMAS rule. One can use parentheses to avoid ambiguity in creating any arithmetic expression.

Conditional expression

A conditional expression compares two values and returns a logical value in the form of True or False.

R has standard operators for comparing values and operators for combining conditions:

Operands

Operators

==

Equality

>(>=)

Greater than (greater than equal to)

<(<=)

Less than (less than equal to)

!=

Inequality

&&

Logical AND

||

Logical OR

!

Logical NOT

For example:

10>5, when executed, returns True.

5>10, when executed, returns False.

Functional call expression

The most common and useful type of R expression is calling functions. There are a lot of built-in functions in R, and users can built their own functions. In this section, we will see the basic structure of calling a function.

A function call consists of a function name followed by parentheses. Within the parentheses, arguments are present, separated by commas. Arguments are expressions that provide the necessary information to the functions to perform the required tasks. An example will be provided when we discuss how to construct user-defined functions.

Symbols and assignments

R code consists of keywords and symbols.

A symbol is the label for an object stored in RAM, and it gets the stored value from the memory when the program gets executed.

R also stores many predefined values for predefined symbols, which is used in the program as required and gets automatically downloaded.

For example, the date() function produces today's date when executed.

The result of an expression can be assigned to a symbol, and it is assigned by using the assignment operator <-.

For example, the expression value <-4+6 assigns the symbol value with value 10 and is stored in memory.

Keywords

Some symbols are used to represent special values and cannot be reassigned:

  • NA: This is used to define missing or unknown values
  • Inf: This is used to represent infinity. For example, 1/0 produces the result infinity
  • NaN: This is used to define the result of arithmetic expression which is undefined. For example, 0/0 produces NaN
  • NULL: This is used to represent empty result
  • TRUE and FALSE: These are logical values and are generally generated when values are compared

Naming variables

When writing R code, we need to store various pieces of information under many symbols. So we need to name these symbols meaningfully as that will make the code easy to understand. Symbols should be self-explanatory. Writing short symbol name will make the code tougher to understand.

For example, if we represent date of birth information by DateOfBirth or DOB, then the first option is better as it is self-explanatory.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime