HomeTutorialsR Programming

# Basic Programming Skills in R

Practice basic programming skills in R by using course material from DataCamp's free Model a Quantitative Trading Strategy in R course.
Oct 2018  · 5 min read

If you want to take our free Model a Quantitative Trading Strategy in R course, here is the link.

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:

``````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 `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.
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuIiwic2Ft cGxlIjoiXG4jIFRoaXMgaXMgZXhhbXBsZSBjb2RlIHRvIHNob3cgeW91IGhv dyB0byByZWFkIGEgLmNzdiBmaWxlXG5kdW1te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b2JqZWN0KFwiZGF0YV9tYXJ1dGlcIixpbmNvcnJlY3Rf bXNnID0gXCJUaGUgPGNvZGU+ZGF0YV9tYXJ1dGk8L2NvZGU+IHZhcmlhYmxl IGlzIG5vdCBpbml0aWFsaXplZCBhcyBleHBlY3RlZC4gVGFrZSBhIGxvb2sg YXQgdGhlIGhpbnQuXCIpXG50ZXN0X2Z1bmN0aW9uKFwiaGVhZFwiLCBcInhc Iiwgbm90X2NhbGxlZF9tc2cgPSBcIlVzZSB0aGUgZnVuY3Rpb24gPGNvZGU+ aGVhZCgpPC9jb2RlPiB0byBpbnNwZWN0IHRoZSBmaXJzdCByb3cgb2YgPGNv ZGU+ZGF0YV9tYXJ1dGk8L2NvZGU+LlwiLCBpbmNvcnJlY3RfbXNnID0gXCJN YWtlIHN1cmUgdG8gcGFzcyB0aGUgPGNvZGU+ZGF0YV9tYXJ1dGk8L2NvZGU+ IHZhcmlhYmxlIHRvIHRoZSBmdW5jdGlvbiA8Y29kZT5oZWFkKCk8L2NvZGU+ LiBUcnkgYWdhaW4uXCIpXG5zdWNjZXNzX21zZyhcIkdyZWF0ISBDaGVjayBv dXQgdGhlIGNvbHVtbiBuYW1lcyBvZiB5b3VyIGRhdGEgc2V0LiBXaGF0IGRv IHlvdSB0aGluayB0aGV5IG1lYW4/XCIpIiwiaGludCI6IlxuUmVhZCB0aGlz IGh0dHA6Ly93d3cuci10dXRvci5jb20vci1pbnRyb2R1Y3Rpb24vZGF0YS1m cmFtZS9kYXRhLWltcG9ydC4ifQ==

## 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()`.
eyJsYW5ndWFnZSI6InIiLCJ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WFkKClg YCBvdmVyIGBkYXRhX21hcnV0aWAsIHdlIHNpbXBseSB3cml0ZSBgYGhlYWQo ZGF0YV9tYXJ1dGkpYGAuIn0=

## 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.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuZGF0YV91 cmwgPSBcImh0dHA6Ly9hc3NldHMuZGF0YWNhbXAuY29tL2NvdXJzZS9xdWFu dGluc3RpL2RhdGFfY2gxLmNzdlwiXG5kYXRhX21hcnV0aSA9IHJlYWQuY3N2 KGZpbGUgPSBkYXRhX3VybCwgaGVhZGVyID0gVFJVRSwgc3RyaW5nc0FzRmFj dG9ycyA9IEZBTFNFKSIsInNhbXBsZSI6IlxuIyBQcmludCB0aGUgY2xhc3Mg b2YgJ2RhdGFfbWFydXRpJ1xuXG5cbiMgUHJpbnQgdGhlIHR5cGUgb2YgJ2Rh dGFfbWFydXRpJ1xuXG5cbiMgUHJpbnQgdGhlIGRpbWVuc2lvbnMgb2YgJ2Rh dGFfbWFydXRpJyIsInNvbHV0aW9uIjoiXG4jIFByaW50IHRoZSBjbGFzcyBv ZiAnZGF0YV9tYXJ1dGknXG5jbGFzcyhkYXRhX21hcnV0aSlcblxuIyBQcmlu dCB0aGUgdHlwZSBvZiAnZGF0YV9tYXJ1dGknXG50eXBlb2YoZGF0YV9tYXJ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GlvbnMgYXMgeW91IGRpZCBpbiBwcmV2 aW91cyBxdWVzdGlvbi4ifQ==

Check out DataCamp's Objects and Classes in R tutorial.

Topics

Course

### .css-1531qan{-webkit-text-decoration:none;text-decoration:none;color:inherit;}Introduction to R

4 hr
2.7M
Master the basics of data analysis in R, including vectors, lists, and data frames, and practice R with real data sets.
See Details
Start Course

Course

### Intermediate R

6 hr
598.5K
Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Course

### Introduction to Data Visualization with ggplot2

4 hr
137.1K
Learn to produce meaningful and beautiful data visualizations with ggplot2 by understanding the grammar of graphics.
See More
Related

blog

### 5 R Challenges to Develop Your Skills

Develop your R programming skills with these five R challenges. Test your skills and complete a challenge within seven days!

DataCamp Team

5 min

tutorial

### Algorithmic Trading in R Tutorial

In this R tutorial, you'll do web scraping, hit a finance API and use an htmlwidget to make an interactive time series chart to perform a simple algorithmic trading strategy

Ted Kwartler

15 min

tutorial

### Creating a List in R

Practice Lists in R by using course material from DataCamp's Intro to R course.

Ryan Sheehy

3 min

tutorial

### Data Frames in R

This tutorial takes course material from DataCamp's Introduction to R for Finance course and allows you to practice Data Frames.

Ryan Sheehy

4 min

tutorial

### Factor Levels in R

This tutorial takes course material from DataCamp's free Intro to R course and allows you to practice Factors.

Ryan Sheehy

6 min

code-along

### Exploratory Data Analysis in R for Absolute Beginners

Learn the basics of exploring new datasets in R.

Ishmael Rico

See MoreSee More