Reader small image

You're reading from  Learning Quantitative Finance with R

Product typeBook
Published inMar 2017
Reading LevelIntermediate
PublisherPackt
ISBN-139781786462411
Edition1st Edition
Languages
Right arrow
Authors (2):
Dr. Param Jeet
Dr. Param Jeet
author image
Dr. Param Jeet

Dr. Param Jeet is a Ph.D. in mathematics from one of India's leading technological institute in Madras (IITM), India. Dr. Param Jeet has a couple of mathematical research papers published in various international journals. Dr. Param Jeet has been into the analytics industry for the last few years and has worked with various leading multinational companies as well as consulted few of companies as a data scientist.
Read more about Dr. Param Jeet

PRASHANT VATS
PRASHANT VATS
author image
PRASHANT VATS

Prashant Vats is a masters in mathematics from one of India's leading technological institute, IIT Mumbai. Prashant has been into analytics industry for more than 10 years and has worked with various leading multinational companies as well as consulted few of companies as data scientist across several domain.
Read more about PRASHANT VATS

View More author details
Right arrow

Data types


In any programming language, one needs to store various pieces of information using various variables. Variables are reserved memory locations for storing values. So by creating a variable, one is reserving some space in the memory. You may like to store various types of data types, such as character, floating point, Boolean, and so on. On the basis of data type, the operating system allocates memory and decides what can be stored in reserved memory.

All the things you encounter in R are called objects.

R has five types of basic objects, also known as atomic objects, and the rest of the objects are built on these atomic objects. Now we will give an example of all the basic objects and will verify their class:

  • Character:

    We assign a character value to a variable and verify its class:

            >a <- "hello"
            >print(class(a))
    

    The result produced is as follows:

            [1] "character"
    
  • Numeric:

    We assign a numeric value to a variable and verify its class:

            >a <- 2.5
            >print(class(a))
    

    The result produced is as follows:

            [1] "numeric"
    
  • Integer:

    We assign an integer value to a variable and verify its class:

            >a <- 6L
            >print(class(a))
    

    The result produced is as follows:

            [1] "integer"
    
  • Complex:

    We assign an integer value to a variable and verify its class:

            >a <- 1 + 2i
            >print(class(a))
    

    The result produced is as follows:

            [1] "complex"
    
  • Logical (True/false):

    We assign an integer value to a variable and verify its class:  

            >a <- TRUE
    >print(class(a))
    

    Then the result produced is as follows:

    [1] "logical"
    

The basic types of objects in R are known as vectors and they consist of similar types of objects. They cannot consist of two different types of objects at the same time, such as a vector consisting of both character and numeric.

But list is an exception, and it can consist of multiple classes of objects at the same time. So a list can simultaneously contain a character, a numeric, and a list.

Now we will discuss the common data types present in R and give at least one example for each data type discussed here.

Vectors

Vectors have already been defined. If we want to construct a vector with more than one element, we can use the c() function which combines the elements into a vector, for example:

>a<-"Quantitative" 
>b<-"Finance" 
>c(a,b) 

This produces the following result:

[1] "Quantitative" "Finance"   

Similarly:

>Var<-c(1,2,3) 
>Var 

This produces the following result:

[1] 1 2 3 

Lists

A list is an R object that consists of multiple types of objects inside it, such as vectors and even lists. For example, let's construct a list and print it using code:

#Create a List and print it 
>List1 = list(c(4,5,6),"Hello", 24.5) 
>print(List1) 

When we execute the previous command, it produces the following result:

[[1]] 
[1] 4 5 6 
   
[[2]] 
[1] "Hello" 
 
[[3]] 
[1] 24.5 

We can extract the individual elements of the list according to our requirements.

For example, in the preceding case, if we want to extract the second element:

>print(List1[2]) 

Upon executing the preceding code, R creates the following output:

[[1]] 
[1] "Hello" 

One can merge the two lists using the function c(); for example:

>list1 <- list(5,6,7) 
>list2 <- list("a","b","c") 
>Combined_list <-c(list1,list2) 
>print(Combined_list) 

Upon executing the preceding command, we get the combined list:

[[1]] 
[1] 5 
 
[[2]] 
[1] 6 
 
[[3]] 
[1] 7 
 
[[4]] 
[1] "a" 
 
[[5]] 
[1] "b" 
 
[[6]] 
[1] "c" 

Matrices

A matrix is a two-dimensional rectangular dataset, and it is created by vector input to the matrix() function.

For example, create a matrix with two rows and three columns, and print it:

>M <- matrix(c(1,2,3,4,5,6), nrow = 2, ncol = 3) 
>print(M) 

When we execute the preceding code, it produces the following result:

     [,1] [,2] [,3] 
[1,]    1    3    5 
[2,]    2    4    6 

Arrays

Matrices are confined to only two dimensions, but arrays can be of any dimension. The array() function takes a dim attribute, which creates the needed dimensions.

For example, create an array and print it:

>a <- array(c(4,5),dim = c(3,3,2)) 
>print(a) 

When we execute the previous code, it produces the following result:

, , 1 
     [,1] [,2] [,3] 
[1,]    4    5    4 
[2,]    5    4    5 
[3,]    4    5    4 
 
, , 2 
 
     [,1] [,2] [,3] 
[1,]    5    4    5 
[2,]    4    5    4 
[3,]    5    4    5 

Factors

Factors are R objects that are created using a vector. It stores the vector along with the distinct elements present in the vector as labels. Labels are always in character form, irrespective of whether it is numeric, character, or Boolean.

Factors are created using the factor() function, and the count of levels is given by n levels; for example:

>a <-c(2,3,4,2,3) 
>fact <-factor(a) 
>print(fact) 
>print(nlevels(fact)) 

When the preceding code gets executed, it generates the following results:

[1] 2 3 4 2 3 
Levels: 2 3 4 
[1] 3 

DataFrames

DataFramesare tabular-form data objects where each column can be of different form, that is, numeric, character, or logical. Each column consists of a list of vectors having the same length.

DataFrames are generated using the function data.frame(); for example:

>data <-data.frame( 
>+Name = c("Alex", "John", "Bob"), 
>+Age = c(18,20,23), 
>+Gender =c("M","M","M") 
>+) 
>print(data) 

When the preceding code gets executed, it generates the following result:

  Name Age Gender 
1 Alex  18      M 
2 John  20      M 
3  Bob  23      M 
Previous PageNext Page
You have been reading a chapter from
Learning Quantitative Finance with R
Published in: Mar 2017Publisher: PacktISBN-13: 9781786462411
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.
undefined
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 $15.99/month. Cancel anytime

Authors (2)

author image
Dr. Param Jeet

Dr. Param Jeet is a Ph.D. in mathematics from one of India's leading technological institute in Madras (IITM), India. Dr. Param Jeet has a couple of mathematical research papers published in various international journals. Dr. Param Jeet has been into the analytics industry for the last few years and has worked with various leading multinational companies as well as consulted few of companies as a data scientist.
Read more about Dr. Param Jeet

author image
PRASHANT VATS

Prashant Vats is a masters in mathematics from one of India's leading technological institute, IIT Mumbai. Prashant has been into analytics industry for more than 10 years and has worked with various leading multinational companies as well as consulted few of companies as data scientist across several domain.
Read more about PRASHANT VATS