Familiarize with R-Programing Language and coding in R-Studio.

Kush
12 min readSep 12, 2021

--

Self Introduction

Hello, Readers I am an aspirant in Data Science and AI from India and I Welcome You to My Journey in R.

The article is based on my documentation in my learning journey with the Basics In R-Programing Language. I have made the best possible attempt to simplify all topics by breaking them into a sequential and orderly manner especially for those who are nonprogrammers and wish to learn the R language from its basics. But to learn R programing language it is always recommended to have some basic knowledge of Math and Statistics before starting programming, just in case if you haven't learned it you can always learn your Math and Statistics during your programming journey with R programming languages just the way I’m doing. So let's begin.

Topics I’ll be covering in this article are:

  • Introduction To R-Language.
  • Installation Of R and R-Studio.
  • Introduction to R-Studio and its interface.
  • Installing and Activating/Deactivating R-Package.
  • Working with Directories.
  • Basic Operations in R-Language.
  • Variables.
  • Creating Variables In R.

The article is sufficient to get you kick-started with installation and coding in R and make you user-friendly with R-Studio. So follow along and start your journey with me.

A Small Introduction To R Language before we proceed.

R Language is a Programing Language used for Statistical Computing and Graph Supporting. R is amongst the favorite language of Statisticians, Data Scientists, Data Engineers, and Data analysts after Python.

Small History of R Language:

R language is an implementation of S programing language implementation begun in the year 1991 by Ross Ihaka and Robert Gentlemen at the University of Auckland New Zealand and it was being publicized in the year 1993 and was named after the two Authors As R.

Who uses R Language?

R has gained popularity in 2021 with the rising demand of Data professionals and the growing implementation of Artificial Intelligence all of its applications are almost similar to that of Python but better use in Data Analytics and Data Mining.

How Much is R useful for a beginner aspirant Data Scientist? :

Answer Lies in the question itself in today’s time R professionals are amongst the most highly paid professionals in Market, in my view a beginner must not skip the language as it deepens once knowledge to their subject and strengthens their foundation, so you can say R is not an Overhyped word it is very useful if you are aspiring for Data Science and at any stage of Learning or a Profession.

For a more detailed history of R you can always refer to the link below :

Wikipedia on R

Let us understand how to install R and R-Studio on our Systems.

To install R please click on the link below.

Install R

Once clicked on the link above you will reach to CRAN page.

The full form of CRAN is Comprehensive R Archive Network.

The page would look as shown in the image below.

Now to avoid complications I am providing links to the latest version of R for Windows/Mac/Linux :

R 4.1.1 Windows

R 4.1.1 MacOS

R 4.1.1 Linux

My Operating system is Windows so I’ll be covering installation with the windows operating system. To Install R on other Operating systems procedures are going to be the same.

To download R on windows click the following link shown on Image :

Once the R Installer Exe is downloaded right click on exe and run it as administrator Follow normal installation procedure, or Create a new folder in your C drive name it as R, and install R in that folder. Check out These Images.

Normal Installation Directory:

Custom Installation :

In the next step select your user installation. I am using 64-bit :

Uncheck 32-bit if you wish to install 64 bit only.

In the Next step Accept the Default and press next with No still selected:

In this Step, if you don't wish to create a start menu folder leave the button below unchecked :

Square Button below

In this Step leave the window as default and click next :

R starts installing and once done you are good to go :

Let’s Install R-Studios IDE Now. (IDE stands for Integrated Development Environment).

Install R-Studio with the Link given below.

R-Studio

Once you click on the link you will come across a window as shown in the image

Click and download exe.

MacOS, Linux, and users of other operating system options to download are available as you scroll down on the same page it will look as shown in the image below.

NOTE: Always download R-Studio free when you are learning as highlighted in the image.

Installation Procedure of R-Studio is common but I prefer to Install R-Studio in the same folder where I have installed R that is C Drive R Folder created especially to install R Language.

Interface Of R-Studio

Before we start let’s take a quick introduction to the Interface of R-Studio.

After you’ve installed R-Studio your interface looks like this👇

Notice how the screen is divided into four areas or windows. One by one we shall see them all.

Beginning with the two windows at left.

Intro to R-Studio Interface :

The Image above highlighted in the red upper left of R-Studio upper left is called Code Editor or source the are is for writing code, editing, and execution of code.

The result of executed code will appear in Console which is on the lower left of R-Studio see the image below highlighted in green.

First Code in R :

Let's print a code to console using the print command and message between double quotations in brackets

eg. print(“Hi And Let’s Begin Our Journey In R”) once you’ve written that press control enter to execute the code.

ref image:- Image below shows how our code is executed in Console.

You can also execute the code by pressing Run on the bar of Code Editor.

See the highlight on the image.

The console can also be used to write and run commands. not only to display the output.

run the same command print(“Hi And Let’s Begin Our Journey In R”) (copy-paste command) in the console and see the output after pressing enter to execute it.

it is never recommended to use the console to write extended codes but can be used to write and execute smaller commands.

Note: R files are saved with an extension capital R, saving and other options remain same you can use ctrl + s for save or 💾 disk symbol on top of code editor.

To open a new tab in code editor we have to press ctrl + shift + n this will open a new code editor tab with a new source on the upper left.

  • To write any comment in the code editor, we will have to use #.
  • To clear the console, we need to press ctrl + l.

That’s it for Source and Code Editor lets check the left window.

Environment and History :

On the top right of the R-Studio IDE, there are four options respectively.

  • Environment.
  • History.
  • Connections.
  • Tutorial.

ref image :

The History Tab carries the list command of commands that are executed in the current session. I’m

Connections and Tutorials tab carries all necessary hyperlinks to learn R-Studio if you wish to take

a deep dive.

To Clear your History tab press the brush button on the top right of the tab. See Image For Details.

Notice two buttons on top of the History tab To Console and Source these are used for moving executed commands to Source editor or Console. Check the Video for detail.

Let us jump to the last tab lower right. .

In the lower right, there are 5 Tabs respectively

  • File.
  • Plot.
  • Package.
  • Help.
  • Viewer.

File

The file tab is used to browse and Navigate folders on our system.

Plot

The plot is useful to display Graphs and Charts that we shall create during our journey.

Package

The package we shall cover is below.

Help & Viewer

  • Under the Help tab, we can see the help files of the Program.
  • Viewers are also useful to display charts like Plot Tab. But in special plotting packages like GGV’s or you can say ggVennDiagram.

Let's get into little detail of Package Tab as Package Tab is the most important Tab

The package tab helps in Updating and Installing all necessary packages required in R programming. There are over 2000 packages in all these packages that help in performing various types of tasks and analysis. anyways In the beginning I am not going into depth about packages as I progress I shall slowly get into its detail.

In the video below, I have shown how to search and install a package in R-Studio.

This second video shows how the package gets installed.

After the package is installed we need to activate the package with respective commands

library(ggvennDiagram)

require(ggvennDiagram)

once typed in Source editor press Ctrl + Enter to execute the command, Check Video for detail.

To deactivate the package, you either have to uncheck the package or type

detach(package : ggVennDiagram)

Working Directory

To access a CSV file or a text file, it is important to set the working directory. let’s learn how that is done.

Look for a Menu called Sessions Menu in Sessions Menu look for Choose Directory. or press ctrl +shift + h, and open the directory you wish to access see the below image for detail.

Once the directory is selected Console tab shows a message as in the image below.

Working Directory can also be selected with a command setwd()

A small Video Reference.

You can also change R-Studio Themes, Fonts, and Font size according to your preferences from the Tools menu by selecting the Global Option, and from the Global Options select Appearance.

Check out this video for more.

For this article, my Theme will remain to Default that is Chrome.

Let’s Begin Coding

Basic Operations in R

R Program is useful in many operations from basic arithmetic to complex computations anything is possible with R. below in the Image I have performed few basic operations with the R program like adding two numbers.

Addition 3+6, Result can be seen in Console

We can do Subtractions.

Subtraction 10–2, Result displayed in Console

Multiplications

Multiplication 3*8, Result in the console.

Divisions

Divisions 12/4, Result in console

Raisings to the Power

Raisings To The Power 5², Result in the console.

We can change the order of the operations using parentheses.

Use of Parentheses (7+3)*9, Result in Console

Nesting multiple Parentheses.

Nesting Multiple Parentheses ((3–2)*5+(10–5)/3)³, Result in the console.

Variables

What are Variables?

If we go according to the basic universal definition Variables are containers for storing data values.

Note: R does not have a command for declaring a variable. A variable is created the moment you first assign a value to it you will learn about that along the way.

Creating Variables

There are three types of variables in R.

  • Numeric.
  • Character (String).
  • Logical.

A variable name must start with a letter it can contain letters, figures, and underscores it cannot contain dashes and other special characters like #, $, %, &, *, \, / etc.

In R we can use the equal sign (=) to assign values to the variables but symbol ( <- ) is often preferred and is also advised to use in R.

in Symbol <- “arrow” directs to the variable name

let's take a small example

x <- 5

in the example, x is the variable name in which the value 5 is stored.

similarly

y <- 7

Now we can perform mathematical operations on variables. as shown in the image below.

We can also store results in a separate variable.

The Class and type of Variables.

The Class represents the abstract type of object and the Type represents how the object is stored in memory.

Class of x is numeric and Type is double. as shown in Image

Type is double because R uses the double-precision floating-point format to store data.

Creating String Variables

To create a string variable we use the double-quotes. as in image

String Variable

Second String Variable

See the Image.

Logical Variables

Logical values are of two types TRUE and FALSE let's see this in the image example.

Logical Variables

We can perform mathematical operations with Logical Values. TRUE is assimilated to 1 and FALSE to 0.

Note: Variable Names are very case sensitive in R also remember R particularly itself is a case-sensitive language.

Image shows case-sensitivity and how R store’s the Values

The image below shows case sensitivity in strings.

The image clearly describes Case Sensitivity how the error message was printed.

List of Variables.

To get the list of variables in the environment, there is a special function ls() ‘ see the image below

Remove Variable

To remove a variable from the environment we use a function rm()

See Video for details

Type rm() and in Parentheses mention the variable you wish to remove for example

rm(salary)

To remove 2 or more variables we have to separate them with a comma. like rm(a, X) see video for more.

To remove all variables from the environment. we have to type

rm(list=ls())

check the video for more

This is it for now.

This is it, for now, thank you for reading this far, and if you have followed along with me just have a look at your progress and be proud of yourself if you do not know anything about coding. I have another similar article On R if you wish you can have a look over it, the link to that is provided below.

Matrices in R With The matrix() Function

As a beginner to programming if I have made any mistakes or missed out on any point in my article my sincere apologies for that, and I am always open to suggestions, and valuable advice to improve my skills Also if you liked it do give it a share with others.

If you have any suggestions for me, or you wish to connect check out the links below. My DM on Twitter is always open.

Twitter

Polywork

Don’t forget to share if you liked it, So until we meet Again Happy Reading And Happy Learning.

--

--

Kush

Code Newbie Data Science and AI Aspirant From India.