Skip to main content
HomeAbout RLearn R

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.

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:

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 dyB0byByZWFkIGEgLmNzdiBmaWxlXG5kdW1teSA8LSByZWFkLmNzdihmaWxl ID0gXCJodHRwOi8vYXNzZXRzLmRhd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ID0gXCJUaGUgPGNvZGU+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().
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IlxuI0Rvd25s b2FkIGRhdGEgc2V0XG5kYXRhX3VybCA9IFwi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9tYXJ1dGkpYGAuIn0=

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 dCB0aGUgdHlwZSBvZiAnZGF0YV9tYXJ1dGknXG50eXBlb2YoZGF0YV9tYXJ1 dGkpXG5cbiMgUHJpbnQgdGhlIGRpbWVuc2lvbnMgb2YgJ2RhdGFfbWFydXRp J1xuZGltKGRhdGFfbWFydXRpKSIsInNjdCI6IlxudGVzdF9vdXRwdXRfY29u dGFpbnMoXCJjbGFzcyhkYXRhX21hcnV0aSlcIiwgaW5jb3JyZWN0X21zZyA9 IFwiVXNlIHRoZSBmdW5jdGlvbiA8Y29kZT5jbGFzcygpPC9jb2RlPiBhbmQg cGFzcyBvbiA8Y29kZT5kYXRhX21hcnV0aTwvY29kZT4gYXMgdGhlIGFyZ3Vt ZW50LlwiKVxudGVzdF9vdXRwdXRfY29udGFpbnMoXCJ0eXBlb2YoZGF0YV9t YXJ1dGkpXCIsIGluY29ycmVjdF9tc2cgPSBcIlVzZSB0aGUgZnVuY3Rpb24g PGNvZGU+dHlwZW9mKCk8L2NvZGU+IGFuZCBwYXNzIG9uIDxjb2RlPmRhdGFf bWFydXRpPC9jb2RlPiBhcyB0aGUgYXJndW1lbnQuXCIpXG50ZXN0X291dHB1 dF9jb250YWlucyhcImRpbShkYXRhX21hcnV0aSlcIiwgaW5jb3JyZWN0X21z ZyA9IFwiVXNlIHRoZSBmdW5jdGlvbiA8Y29kZT5kaW0oKTwvY29kZT4gYW5k IHBhc3Mgb24gPGNvZGU+ZGF0YV9tYXJ1dGk8L2NvZGU+IGFzIHRoZSBhcmd1 bWVudC5cIilcbnN1Y2Nlc3NfbXNnKFwiVGhpcyBpcyBnb2luZyB3ZWxsISBX ZSBoYXZlIG5vdyBjb25maXJtZWQgdGhhdCA8Y29kZT5kYXRhX21hcnV0aTwv Y29kZT4gaXMgaW4gZmFjdCBhIGRhdGEgZnJhbWUuIExldCB1cyBub3cgbGVh cm4gdG8gdXNlIHNvbWUgZnVuY3Rpb25zIGluIFIgdG8gY2FsY3VsYXRlIHJl dHVybnMhXCIpIiwiaGludCI6IlxuVGhpcyBpcyBmYWlybHkgYmFzaWMhIEp1 c3QgdXNlIHRoZSBnaXZlbiBmdW5jdGlvbnMgYXMgeW91IGRpZCBpbiBwcmV2 aW91cyBxdWVzdGlvbi4ifQ==


If you want to learn more from this course, here is the link.

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

Topics

Learn more about R

Certification available

Course

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 DetailsRight Arrow
Start Course
See MoreRight Arrow
Related

Data Science in Finance: Unlocking New Potentials in Financial Markets

Discover the role of data science in finance, shaping tomorrow's financial strategies. Gain insights into advanced analytics and investment trends.
 Shawn Plummer's photo

Shawn Plummer

9 min

5 Common Data Science Challenges and Effective Solutions

Emerging technologies are changing the data science world, bringing new data science challenges to businesses. Here are 5 data science challenges and solutions.
DataCamp Team's photo

DataCamp Team

8 min

Navigating R Certifications in 2024: A Comprehensive Guide

Explore DataCamp's R programming certifications with our guide. Learn about Data Scientist and Data Analyst paths, preparation tips, and career advancement.
Matt Crabtree's photo

Matt Crabtree

8 min

A Data Science Roadmap for 2024

Do you want to start or grow in the field of data science? This data science roadmap helps you understand and get started in the data science landscape.
Mark Graus's photo

Mark Graus

10 min

R Markdown Tutorial for Beginners

Learn what R Markdown is, what it's used for, how to install it, what capacities it provides for working with code, text, and plots, what syntax it uses, what output formats it supports, and how to render and publish R Markdown documents.
Elena Kosourova 's photo

Elena Kosourova

12 min

Introduction to DynamoDB: Mastering NoSQL Database with Node.js | A Beginner's Tutorial

Learn to master DynamoDB with Node.js in this beginner's guide. Explore table creation, CRUD operations, and scalability in AWS's NoSQL database.
Gary Alway's photo

Gary Alway

11 min

See MoreSee More