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 http://assets.datacamp.com/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:
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
dummy data frame uploaded as an example for you. Notice that there are three arguments in the read.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 (TRUE in this case) and stringsAsFactors, which specifies whether or not strings in the table should be saved as factors (FALSE in this case).
- Create a new variable
data_maruti that contains Maruti's daily data from 1st Jan 2008 to 31st Dec 2013. Make sure to specify header as TRUE and stringAsFactors as FALSE.
- Finally, inspect the first rows of the data frame you have loaded using the function
head(). Simply run head(data_maruti) to call the function.
# This is example code to show you how to read a .csv file
dummy <- read.csv(file = "http://assets.datacamp.com/course/quantinsti/data_ch1.csv", header = TRUE, stringsAsFactors = FALSE)
# Read out the .csv file saved at 'http://assets.datacamp.com/course/quantinsti/data_ch1.csv'
data_maruti <-
# Inspect the data frame
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_maruti using the function tail().
- Print the numbers of rows in
data_maruti using the function nrow().
- Print the number of columns in
data_maruti using the function ncol().
# Print last six rows of 'data_maruti'
# Print the number of rows in `data_maruti`
# Print the number of columns in `data_maruti`
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.
# Print the class of 'data_maruti'
# Print the type of 'data_maruti'
# Print the dimensions of 'data_maruti'
If you want to learn more from this course, here is the link.
Check out DataCamp's Objects and Classes in R tutorial.
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 http://assets.datacamp.com/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
objectin R, calleddata_maruti. The syntax for defining a newvariableorobjectis very straightforward:where
variable_nameis the name of the newobjectorvariableandvalueis the value assigned to it.We will save a .csv data file in
data_maruti. Thefunctionused to read a .csv file and save it as a data frame isread.csv(). The syntax for calling a function is quite simple:where
function_nameis the name of thefunctionandobject1,object1are the objects or variables on which you want to run the function.argumentsare 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
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).data_marutithat contains Maruti's daily data from 1st Jan 2008 to 31st Dec 2013. Make sure to specifyheaderasTRUEandstringAsFactorsasFALSE.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 ofdata_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_marutidata frame is already loaded in the workspace for you.Instructions
data_marutiusing the functiontail().data_marutiusing the functionnrow().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_marutiusing functionstypeof(),class(), anddim(), respectively. The data framedata_marutiis 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.