Our Support: During the COVID-19 outbreak, we request learners to CALL US for Special Discounts!

- Data Science Blogs -

R Programming for Data Science Tutorial Guide for Beginner

Introduction to R for Data Science

Data science can be defined as the discipline of using raw data as input and extracting knowledge and insights from it.The main objective of “R for data science” is that it help you to learn the most important tools in R that will permit you to do data science.

R programming language, developed by Ross Ihaka and Robert Gentleman in 1993, is widely used for applications related to data science. R provides support for an extensive suite of statistical methods, inference techniques, machine learning algorithms, time series analysis, data analytics, graphical plots to list a few. These features make it a great language for data exploration and investigation. 

Read: Data Science vs Machine Learning - What you need to know?

Before we proceed further with programming in r for data science and what is r for data science? Let’s first discuss what is data science and what is a data scientist?

What is data science?

Data science is the study of data that involves developing methods of analyzing, recording and storing data to effectively extract useful information.The main aim of data science is to get in-depth knowledge about any type of structured and unstructured data.

What is a data scientist?

A data scientist is one who has technical skills to solve complex problems and who has curiosity to explore what kind of problems are needed to be solved. The main goal of data scientists is to analyze, process, and model data then interpret the outcomes to create actionable plans for companies and other organizations.

Data Science Training - Using R and Python

  • Detailed Coverage
  • Best-in-class Content
  • Prepared by Industry leaders
  • Latest Technology Covered

Getting started with R for Data Science

R can be downloaded from CRAN , the comprehensive R archive network. CRAN comprises a set of mirror servers distributed around the world and is used to distribute R and R packages. A new major version of R comes out once a year, and there are 2 to 3 minor versions each year. RStudio provides an integrated development environment, or IDE, for R programming.

How to install R Packages?

R functionality is provided in terms of its packages. There are now over 10000 R packages in CRAN.

Packages contain R functions, data, and compiled code in a well-defined format. The directory containing the packages is called the library. R ships with a standard set of packages. Non standard packages are available for download and installation. Examples of R packages include arules,ggplot2,caret,shiny etc. Packages can be installed with the install.packages() function as shown below.


This command causes R to download the package from CRAN. Once you have a package installed, you can make its contents available to use in your current R session by using the library command:


You can join our Data Science Demo Class to solve your problems.Just Enroll Now!

Data Science Training - Using R and Python

  • No cost for a Demo Class
  • Industry Expert as your Trainer
  • Available as per your schedule
  • Customer Support Available

What is Programming in R for Data Science?

Let us take a look at a simple program in R which prints “Hello World”. This can be accomplished either from the command line in the R interpreter or via a R script. Let us look at both mechanisms.

Hello World in R – from the R command prompt:

> print("Hello world!")

[1] "Hello world!"

Creating a HelloWorld.R script in R:

helloStr <- "Hello world!"

Read: Data Science vs Machine Learning - What you need to know?


The script can be executed using Rscript HelloWorld.R. It will print:

[1] "Hello world!"

What are different BuiltIn DataSets in R?

R comes with a large number of built in datasets.These can be used as demo data for understanding R packages and functions. Data sets in package 'datasets' include:

AirPassengers Monthly Airline Passenger Numbers 1949-1960
BJsales Sales Data with Leading Indicator
BJsales.lead (BJsales) Sales Data with Leading Indicator
BOD Biochemical Oxygen Demand
CO2 Carbon Dioxide Uptake in Grass Plants
ChickWeight Weight versus age of chicks on different diets
DNase Elisa assay of DNase
EuStockMarkets Daily Closing Prices of Major European Stock Indices, 1991-1998
Formaldehyde Determination of Formaldehyde
HairEyeColor Hair and Eye Color of Statistics Students
Harman23.cor Harman Example 2.3
Harman74.cor Harman Example 7.4
Indometh Pharmacokinetics of Indomethacin
InsectSprays Effectiveness of Insect Sprays
JohnsonJohnson Quarterly Earnings per Johnson & Johnson Share
LakeHuron Level of Lake Huron 1875-1972
LifeCycleSavings Intercountry Life-Cycle Savings Data
Loblolly Growth of Loblolly pine trees
Nile Flow of the River Nile
Orange Growth of Orange Trees
OrchardSprays Potency of Orchard Sprays
PlantGrowth Results from an Experiment on Plant Growth
Puromycin Reaction Velocity of an Enzymatic Reaction
Seatbelts Road Casualties in Great Britain 1969-84
Theoph  Pharmacokinetics of Theophylline
Titanic Survival of passengers on the Titanic
ToothGrowth The impact of Vitamin C on Tooth Growth in Guinea Pigs
UCBAdmissions Student Admissions at UC Berkeley
UKDriverDeaths Road Casualties in Great Britain 1969-84
UKgas UK Quarterly Gas Consumption
USAccDeaths Accidental Deaths in the US 1973-1978
USArrests Violent Crime Rates by US State
USJudgeRatings Ratings of State Judges in the US Superior Court
USPersonalExpenditure Personal Expenditure Data
UScitiesD Distances Between European Cities and Between US Cities
VADeaths Death Rates in Virginia (1940)
WWWusage Internet Usage per Minute
WorldPhones The World's Telephones
ability.cov Ability and Intelligence Tests
airmiles Passenger Miles on Commercial US Airlines, 1937-1960
airquality New York Air Quality Measurements
anscombe Anscombe's Quartet of 'Identical' Simple Linear Regressions
attenu The Joyner-Boore Attenuation Data
attitude The Chatterjee-Price Attitude Data
austres Quarterly Time Series of the Number of Australian Residents
beaver1 (beavers) Body Temperature Series of Two Beavers
beaver2 (beavers) Body Temperature Series of Two Beavers
cars Speed and Stopping Distances of Cars
chickwts Chicken Weights versus Feed Type
co2 Mauna Loa Atmospheric CO2 Concentration
crimtab Student's 3000 Criminals Data
discoveries Yearly Numbers of Important Discoveries
esoph Smoking, Alcohol and (O)esophageal Cancer
euro Conversion Rates of Euro Currencies
euro.cross (euro) Conversion Rates of Euro Currencies
eurodist Distances Between European Cities and BetweenUS Cities
faithful Old Faithful Geyser Data
fdeaths (UKLungDeaths) Monthly Deaths from Lung Diseases in the UK
freeny Freeny's Revenue Data
freeny.x (freeny) Freeny's Revenue Data
freeny.y (freeny) Freeny's Revenue Data
infert Infertility after Spontaneous and Induced Abortion
iris Edgar Anderson's Iris Data
iris3 Edgar Anderson's Iris Data
islands Areas of the World's Major Landmasses
ldeaths (UKLungDeaths) Monthly Deaths from Lung Diseases in the UK
lh Luteinizing Hormone in Blood Samples
longley Longley's Economic Regression Data
lynx Annual Canadian Lynx trappings 1821-1934
mdeaths (UKLungDeaths) Monthly Deaths from Lung Diseases in the UK
morley Michelson Speed of Light Data
mtcars Motor Trend Car Road Tests
nhtemp Average Yearly Temperatures in New Haven
nottem Average Monthly Temperatures at Nottingham, 1920-1939
npk Classical N, P, K Factorial Experiment
occupationalStatus Occupational Status of Fathers and their Sons
precip Annual Precipitation in US Cities
presidents Quarterly Approval Ratings of US Presidents
pressure Vapor Pressure of Mercury as a Function of Temperature
quakes Locations of Earthquakes off Fiji
randu Random Numbers from Congruential Generator RANDU
rivers Lengths of Major North American Rivers
rock Measurements on Petroleum Rock Samples
sleep Student's Sleep Data
stack.loss (stackloss) Brownlee's Stack Loss Plant Data
stack.x (stackloss) Brownlee's Stack Loss Plant Data
stackloss Brownlee's Stack Loss Plant Data
state.abb (state) US State Facts and Figures
state.area (state) US State Facts and Figures
state.center (state) US State Facts and Figures
state.division (state) US State Facts and Figures
state.name (state) US State Facts and Figures
state.region (state) US State Facts and Figures
state.x77 (state) US State Facts and Figures
sunspot.month Monthly Sunspot Data, from 1749 to "Present"
sunspot.year Yearly Sunspot Data, 1700-1988
sunspots Monthly Sunspot Numbers, 1749-1983
swiss Swiss Fertility and Socioeconomic Indicators (1888) Data
treering Yearly Treering Data, -6000-1979
trees Width, Height and Volume for Cherry Trees
uspop Populations Recorded by the US Census
volcano Topographic Information on Auckland's Maunga Whau Volcano
warpbreaks The Number of Yarn Breaks during Weaving
women Average Heights and Weights for American Women

We can view the contents of any of these datasets using the following commands:

Read: Learn Data Science - Get Certified & See an Advancement in Your Career

# Loading 
# Print the first n rows 
head(, )
For example, lets explore the iris datset:
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2  setosa
2 4.9 3 1.4 0.2  setosa
3 4.7 3.2 1.3 0.2  setosa

What is Data ingestion in R?

R is used for data processing and analysis tasks. The first step in this activity is importing data. Base R provides several functions for this purpose. Let us explore some of these for a better understanding:

read.table : Used for importing tab delimited tabular data.


1 2 3
4 5 6
7 8 9
A b c
D e f

  > df <- read.table("data.txt", header = FALSE)

> df

  V1  V2  V3
1 1 2 3
2 4 5 6
3 7 8 9
4 A b c
5 D e f

read.csv : Used for importing csv file with comma(,) delimiter.


1 2 3
4 5 6
7 8 9
A b c
D e f

read.csv2 : Used for importing csv file with semicolon(;) delimiter.

read.delim : Used for importing delimited file with any arbitrary delimiter.

Other library functions are available for importing data of specific format:


  • readxl:read_excel for reading excel files
  • rjson:fromJSON for reading JSON data
  • XML:xmlTreeParse for xml data.
  • RCurl:readHTMLTable for reading HTML table data

Read: A Detailed & Easy Explanation of Smoothing Methods

What is Data Preparation and Cleansing in R?

Eighty percent of data analysis is spent on the cleaning and preparation of data. It is not just the first step, but may need to be repeated many times over the course of analysis.

In tidy data:

  • Each variable forms a column.
  • Each observation forms a row.
  • Each type of observation unit forms a table.

Tidy represents a standard way of structuring a dataset. Real world datasets need not necessarily be available in tidy format:

  • Column headers may not be variable names.
  • Multiple variables might be stored in one column.
  • Variables might be stored in rows.
  • Unrelated observations might be stored in the same table.
  • A single observational unit might be stored across multiple tables.

R provides a package tidyr for converting data into tidy format. tidyr provides three main functions for tidying up messy data:

  • gather(),
  • separate()and
  • spread().

gather() takes multiple columns, and organizes them into key-value pairs.

For example, consider the dataset below which represents the test scores in 2 tests(a and b) for 3 individuals named Amar, Akbar and Anthony:


messy <- data.frame( name = c("Amar", "Akbar", "Anthony"), a = c(56, 91, 88), b = c(72, 64, 60) ) messy #>    name    a  b
#> 1  Amar   56 72
#> 2 Akbar   91 64
#> 3 Anthony 88 60

But ths dataset is currently not in a tidy format (Variables must correspond to columns). For it to be converted it into column format the data must be represented as name , test , score.

Let us see how we can use tidyr package to convert the existing dataset into tidy form.

messy %>%
  gather(test, score, a:b)
#>      name   test     score
#> 1   Amar    a        56
#> 2   Akbar   a        91
#> 3   Anthony a        88
#> 4   Amar    b        72
#> 5   Akbar   b        64
#> 6   Anthony b        60

Here we used the pipe operator %>%. The pipe operator allows you to pipe the output from one function to the input of another function. In our case the messy dataframe is piped as input to the gather function.

Similarly, separate function allows us to separate two variables are clumped together in one column.

spread(), takes two columns (key-value pair) and spreads them in to multiple columns, making data wider.

The tidied dataset can then be transformed as per the requirement of analysis.

R provides several packages for data transformation. Let us look at one of these – dplyr.

Read: An Easy to Understand the Definition of the Confidence Interval

Below are some of the functions which are useful for this purpose:

  • filter : Pick observations by their values
  • arrange : Reordering the rows
  • select : Pick variables by their names
  • mutate : Create new variables in terms of functions of existing variables
  • summarise : Create a single summary value from multiple given values
  • group_by() : grouping operations in the “split-apply-combine” concept

For example let us determine all the entries in the iris datset with Species as ‘virginica’ and Sepal.Width>3:

> library(dplyr)

> filter(iris,Species=="virginica",Sepal.Width>3)

Sepal.Length Sepal.Width Petal.Length Petal.Width   Species

1 6.3 3.3 6 2.5 virginica
2 7.2 3.6 6.1 2.5 virginica
3 6.5 3.2 5.1 2.0 virginica
4 6.4 3.2 5.3 2.3 virginica
5 7.7 3.8 6.7 2.2 virginica
6 6.9 3.2 5.7 2.3 virginica
7 6.7 3.3 5.7 2.1 virginica
8 7.2 3.2 6 1.8 virginica
9 7.9 3.8 6.4 2.0 virginica
10 6.3 3.4 5.6 2.4 virginica
11 6.4 3.1 5.5 1.8 virginica
12 6.9 3.1 5.4 2.1 virginica
13 6.7 3.1 5.6 2.4 virginica
14 6.9 3.1 5.1 2.3 virginica
15 6.8 3.2 5.9 2.3 virginica
16 6.7 3.3 5.7 2.5 virginica
17 6.2 3.4 5.4 2.3 virginica

What is Data Modeling in R?

A model provides a simple low-dimensional summary of a given dataset. R provides inbuilt functions that make fitting statistical models very simple.

Read: How to work with Deep Learning on Keras?

The function to fit linear models is called lm. It is very useful for regression analysis of dataset.The generic syntax is as follows:

> fitted_model <- lm(formula, data = data.frame) For example: > fitted_model <- lm(y ~ x1 + x2 + x3, data = production) Will fit a multiple regression model of dependent variable y on independent variables x1,x2 and x3. Generalized linear models extend linear models to distributions such as gaussian, binomial, poisson, inverse gaussian,gamma and quasi-likelihood models. > fitted_model <- glm(formula, family=family.generator, data=data.frame) `For example: > fitted_model <- glm(y ~ x1 + x2, family = gaussian, data = production) Or > fitted_model <- glm(y ~ x, family = binomial(link=probit), data = mydata) Or > fitted_model <- glm(y ~ x1 + x2 - 1, family = quasi(link=inverse, variance=constant), data = testdata) The model parameters can be visualized by calling > summary(fitted_model)

## Call:
## glm(formula = formula, family = "binomial", data = mydata)
## ## Deviance Residuals: 
##     Min       1Q   Median       3Q      Max  
## -2.6456  -0.5858  -0.2609  -0.0651   3.1982  
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                0.07882    0.21726   0.363  0.71675    
## age                        0.41119    0.01857  22.146  

Besides these R also provides support for other models such as :

  • Classification and Regression model – caret package
  • Mixed models – nlme package
  • Robust Regression – package MASS ( removes outliers)
  • Additive models – package acepack
  • Tree models – package rpart,tree

What is Data Visualization in R?

Data visualization is an important aid in data analysis and decision making.ggplot2 is a data visualization package for R. ggplot2 is an implementation of Grammar of Graphics(gg)—a general scheme for data visualization which breaks up graphs into components such as scales and layers. In contrast to base R graphics using plot function, ggplot2 allows the user to add, remove or alter components in a plot at a high level of abstraction.

ggplot(dat, aes(year, lifeExp)) + geom_point()

This will create a graph between year and life expectancy data from the dataset dat and depict it using geometric points on the graph.

Read: R Programming Language Interview Questions & Answers

Different types of plots can be created by making use of additional graphing primitives such as geom_lines(),geom_boxplot(),geom_smooth() etc.

qplot is a convenient wrapper on tip of ggplot2 for creating a number of different types of plots .

The generic syntax for qplot is :

qplot(x, y, ..., data, facets = NULL, margins = FALSE, geom = "auto", xlim = c(NA, NA), ylim = c(NA, NA), log = "", main = NULL, xlab = NULL, ylab = NULL, asp = NA, stat = NULL,position = NULL)


x, y, ... Aesthetics passed into each layer
data Data frame to use (optional). If not specified, will create one.
facets faceting formula to use.
margins See facet_grid: display marginal facets?
geom Character vector specifying geom(s) to draw. Default: "point" if both x and y are specified, "histogram" if only x is specified.
xlim, ylim X and y axis limits
log variables to log transform ("x", "y", or "xy")
main, xlab, ylab Character vector/expression giving plot title, x axis label, and y axis label.
asp The y/x aspect ratio
stat, position DEPRECATED.


qplot(mpg, wt, data = mtcars)

f <- function() {
   a <- 1:10
   b <- a ^ 3
   qplot(a, b)

This will plot a curve with a[1-10] on x-axis and b=a^3 on y axis and the (x,y) pairs being represented by points.  

“You can be an R-programming professional by Enrolling Today”

Data Science Training - Using R and Python

  • Personalized Free Consultation
  • Access to Our Learning Management System
  • Access to Our Course Curriculum
  • Be a Part of Our Free Demo Class


R as a language is developed from ground up for data analysis and data interpretation. As is rightly said, data represents power in the new economy. But we need appropriate tools to harness the power inherent in raw data. R programming for data science provides us with this power. With an ever growing user community and expanding package list covering all facets of data science, R is a language of choice for data science. This post provides a brief introduction to R and its capabilities so that readers can get started quickly and begin exploring further all the powerful features available for data modelling and interpretation.

Read: Difference Between Data Scientist and Data Analyst

    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

-1 day 18 Sep 2020


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

7 days 26 Sep 2020

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

5 days 24 Sep 2020


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

-1 day 18 Sep 2020


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

0 day 19 Sep 2020


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

-1 day 18 Sep 2020

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

0 day 19 Sep 2020

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

0 day 19 Sep 2020


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

-1 day 18 Sep 2020

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

5 days 24 Sep 2020

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

8 days 27 Sep 2020


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

-1 day 18 Sep 2020

Search Posts


Receive Latest Materials and Offers on Data Science Course