An Introduction to R Programming

  • Lalit Salunkhe
  • Jun 08, 2020
  • R Programming
An Introduction to R Programming title banner

For some reason, I always felt that the field of data analysis, statistical analysis, and data science will evolve around the R Programming language. Because, it is a programming language that was specifically designed for statistical analysis, scientific computing, and nice, visually appealing graphics during the early 90s. The language is one of the most popular in the field and is making into the list of top five programming languages used by data scientists for many years.

 

The R programming language is a domain-specific language that is developed for statistical analysis, scientific computing. Despite that, being in the list of top five programming languages itself emphasizes the importance and quality of it.

 

In this article, we will be trying to understand the R Programming Language, it’s syntaxes and some basic operations. Over a period of time, we will discuss the topics one by one associated with the same.

 

Get ready to dive deep into the world of R Programming.

 

 

Why R Programming?

 

There are several reasons for R being so popular among data analysts and data scientists, I will try to mention a few of those to get you a clear idea of it.

 

  1. R programming language is an open-source language. Meaning, you don’t need to pay a penny to use the same. Besides that, there is a rich package library associated with R which is useful as well as sufficient to master all the skills a data scientist aspires to have. 

  2. R runs smoothly on all platforms. It doesn’t really matter if you are on Windows, or Mac, or Linux. R will be running smoothly on any of it. 

  3. Cross-Platform interpretability is something that makes it more interesting and easy to use for the users. Meaning, the code you write in R, can be shared as it is multiple times with other platforms without a hustle and it will still run smoothly with adequate results.

  4. It has high demand in the market due to all the features that we’ve discussed above (I can add more points. But need to stop as there is another part of the content that needs to be covered here). This high demand opens up more opportunities for professionals. This is one of the core reasons people are trying to learn R programming language.

 

In order to start your journey through the world of R Programming, you need to have two things first installed on your system:  R and RStudio. I am not going to dive deep into how to install this two software and steps involved. Instead of that, I am sharing a link to a quick start guide which will help in making this procedure smooth for you. Read how to install R and RStudio on various platforms.

 

Throughout this article and series, we are going to use RStudio to run all the codes, therefore, it becomes important to have a look at the tool, how it looks, and what are the tabs we are going to use.

 

 

RStudio Overview:

 

Launch RStudio on your system. The main screen should look as the one shown in the screenshot below:


Code illustration 1

Code illustration 1


As soon as we open RStudio, we see a tiled layout with 4 tiles or windows.

 

  1. Source: This window allows you to write all the lines of code in a single shot. Also, it offers code saving, script saving options.

  2. Console: This is the most important panel. Once we run the script under Source pane, each line of code gets executed under console and numeric/text outputs will be shown here.

  3. Environment/History/Connections: This is a pane where you can see the list of variables created, databases added, functions called on a specific variable, etc.

  4. Files/Plots/Packages/Help/Viewer: This tab can also be considered as a second output tab. This is where you can see the graphical representations most of the time. You can look and install into packages, take help on certain topics, etc.

 

 

How to use the console to Code?

 

Console under RStudio is something where we can have line to line execution of the code and get output. See the image below for a better representation.


Code illustration 2

Code illustration 2


Here the “>” is a symbol that lets you know the line where code should be written. Once you hit the Enter button, the code gets executed and you’ll see an output below the line. [1] in the output specifies the index and it specifies that the output you are looking at is a first element under output.

 

 

How to code using Script

 

Script is like a notepad where you can write the code with formatting and then run it to see the output under Console.


Code illustration 3

Code illustration 3


To open a new script, you can navigate towards File menu > New File > R Script or you can use a keyboard shortcut Ctrl + Shift + N to open a new script.


Code illustration 4

Code illustration 4


 

Comments in R

 

Comments are a good way to express what you have done under the code written. They allow the user to understand the code written by you. Under R, you can add a comment using hash, pound, or number symbol (#). Anything written after this symbol will be considered as a comment under R.


Code illustration 5

Code illustration 5


 

Arithmetic Operations Under R

 

Arithmetic operations are the base of any programming language. Moreover, they work almost the same under all of the programming languages. Under R as well most of the arithmetic operators work as they are supposed to. See the list of arithmetic operators and the operations they do under R as shown below:
 

Arithmetic Operators

Operations

+

Addition

-

Subtraction

*

Multiplication

/

Division

%/%

Integer Division

^/**

Power/Exponential

%%

Modulo

 

The top four arithmetic operators are the ones that can be used for addition, subtraction, multiplication, and division respectively.

 

See an example as shown below:


Code Illustration 6

Code illustration 6


Integer division operator allows us to get the integer part once we divide a number to the left by a number to the right of the operator. Let’s see we want to divide 8 by 3. The normal division operator will be giving us output as 2.666667. However the integer division operator will return only 2 (the integer part of the actual division).


Code illustration 7

Code illustration 7


The exponential operator raises the power of the number on the left to the number on the right. Please see below example where 4 ^ 3 or 4 ** 3 will return 64 as output. Meaning we are raising the power of 4 by 3 times.


Code illustration 8

Code illustration 8


Modulo operator works in a way that it gives the remainder after the number towards the left of the operator is divided by the number to the right of the operator. For example, if we try 7 modulo 3 (i.e. 7%%3), we will get remainder (which is our output of the modulo operator) as 1.


Code illustration 9

Code illustration 9


 

Creating Variables under R

 

Creating variables and assigning those to a value is the most important task in any programming language. Variables under R are placeholders. They hold certain memories from your system ram and store variables there. Every time you need the values assigned to a variable; all you need to do is just call that variable. It can also be used in the middle of complex code.

 

 

Assignment Operator in R

 

Assignment operator under R is an arrow-like structure “<-”. This operator helps you assign a variable to a value. Variable is placed at the left of this operator and values to which variable gets assigned are at the right side of the operator. Well, conventional equals to “=” operators are also available under R to assign the values. However, it is not recommended to reduce the confusion as we use equals to sign somewhere else in this programming language with some other perspective.


Code illustration 10

Code illustration 10



If you want to see the value stored under a variable, you need to call it out with its name. See an example below:


Code illustration 11

Code illustration 11


You can use the value assigned under the variable by calling out the name of the variable under the code you write. 

 

Code illustration 12

Code illustration 12


In the example above, we have used the addition operator to add 16 and var1. Ideally, we are making an addition between two numbers: 16 and the one assigned under variable named var1 (i.e. 2). Hence, in result we are getting 16 + 2 = 18 as an output.

 

 

Rules for Variable Naming

 

There are certain rules which we need to follow while creating a variable name. 

 

  1. We should not start a variable name with a numeric value. It should always start with an alphabet.

  2. We cannot use special characters asterisk, pound or dollar sign while creating a variable under the R environment. It will throw an error. The only special character that can be used while creating a variable is underscore (“_”)

  3. There is a case sensitivity rule applicable for variables under R. Meaning, “var1” and “Var1” are two different variables.


 

Conclusion

 

In this article, we have tried to cover the introduction to R Programming, it’s demand, why it is so popular, how the layout of RStudio looks like, script, console, etc. We have seen how the arithmetic operations, variable assignment, and creation along with the variable naming rules.


Let us stop here in this article. We will come up with some new as well as the advanced concepts of R Programming in our next article. Until then Stay Indoors, Stay Safe!😊 More or less, this is not an end of learning, for more such amazing analytics techniques follow us regularly on  FacebookTwitter, and LinkedIn

0%

Comments