SlideShare a Scribd company logo
1 of 39
Download to read offline
Introduction
to R
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTER I. Course Overview and
Preliminary Steps
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited
Learningobjectives
• Know what is R and how it works
• Learn basics of working with data in R
• Get familiar with basic commands/functions
• Learn how to do basic analysis on any dataset and be able to
create basic charts
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
What is R & Why we use it
• It’s a tool : Open-Source, cross platform, free programming
language designed to build statistical solutions
• Powerful : Gives access to CRAN repository containing over
10,000 packages with pre-defined functions for almost every
purpose
• Stays Relevant : Constantly being updated by users ( Scientists,
Statisticians, Researchers, Students!)
• More: Makes beautiful graphs, can create custom functions or
modify existing ones, can be integrated into many environments
and platforms such as Hadoop etc
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Installing R
• Can be downloaded for free from
http://www.r-project.org/
• Download the version compatible with your OS
• Simple/Standard installation process
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Installing R -Studio
• Can be downloaded for free from:
• https://www.rstudio.com/products/rstudio/download/
• Download the free version compatible with your OS
• R needs to be installed before installing R- Studio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R-Studio UI
Write your
code here
Global Environment-
See your datasets here
Console - see
your code run
here
See your files,
graphs, help
documentation
and installed
packages here
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R Commands
• Assignments E.g.: x = 1, or x <- 1
• Functions E.g.: print(“Hello World”)
• Computations E.g.: 17 + 3 ; x + 5
• Mix E.g.: y = sqrt(16); y = 15 + 5
• Assignment queries will update objects in your
R environment
• Queries without assignment, as well as ‘call’ of
R objects will either generate an output in the
console, or in the plot tab
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTER II. R Basics: DataTypes
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited
Variable Assignment in R
• A basic construct in programming is "variable"
• A variable allows you to store a piece of data (‘datum’, e.g.
6, ‘Hello’, etc.. ) or several pieces of data of a common
type, and assign them a unique name
• You can then later ‘call’ this variable's name to easily access
the value(s) that is/are stored within this variable.
Careful, R is case sensitive: The variables ‘x’ and ‘X’ can coexist
in R environment and have different values.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Basic data types in R
• R works with numerous data types. The most common
types are:
• Decimals values like 3.5, called 'numeric'
• Natural numbers like 3 are called 'integers'. Integers
are also numeric
• Boolean variables (TRUE or FALSE) are
classified as ‘logical’
• Text (or string) values are classified as
'character’
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Basic data types in R
• Categorical variables are called ‘factors’. They
have a finite and defined set of values they can
take (e.g. eye_color can take have a value
contained in {‘blue’, ‘green’, ’brown’, ‘black’})
• Other variables can contain time data such as
dates, day of the week, hours, minutes, etc..
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTER III. R Basics: Data
Structures
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized
use or distribution prohibited
R Objects:Vectors
• To assign multiple values to a variable, we can use an R
object called a ‘vector’
• A vector is a sequence/collection of data elements of the
same basic type. Members in a vector are officially called
components. For Example: my_vector = c(14,26,38,30)
• To access a specific element in the vector, we simply need
to call variable_name[i], ‘i’ being the element’s position in
the vector. For example: vect[3] would return 38
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R Objects: Matrices
• A matrix is a sequence/collection of data elements of the
same basic type arranged in a two-dimensional
rectangular layout.
• Being a 2-dimensional object, in order to obtain a specific
value within the matrix, 2 coordinates needs to be
entered. For example: my_matrix[i,j] would return the
element on the ith row, in the jth column
• my_matrix[i,] would return the entire ith row
• my_matrix[,j] would return the entire jth column
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R Objects: Data Frames
• A data frame is used for storing data tables. It is a list of
vectors of equal length. Unlike matrices, it can gather
vectors containing different basic types
• Selection of specific elements in data frames works the
same way as for matrices. For example: my_dataframe[i,j]
would return the element on the ith row, in the jth
column
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R Objects: Lists
• A list in R allows you to gather a variety of objects under
one name (that is, the name of the list) in an ordered way.
These objects can be matrices, vectors, data frames, even
other lists, etc. It is not even required that these objects
are related to each other.
• To access the ith object in the list, write my_list[[i]]
• If you want to access a variable in the ith object in the list,
write my_list[[i]] [variable coordinates]. See examples in R
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTER IV. Importing Packages
and Datasets,Viewing Data
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited
R: Packages
• R Packages are collections of R functions and data sets
• Some standard ones come with R installation
• Others can be installed in a few clicks in Rstudio, or
using install.packages(“package name”) function.
You can choose the CRAN Mirror closest to your
location, but the default Rstudio is consistently good
all over the world.
• Some have to be downloaded ( from http://cran.r-
project.org/, or through Google and manually
installed
• Once installed we need to call the package in when
needed using “library(“package name”)”
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R: Importing Data
• More often than not, data is already available in
different formats ready to be imported to R.
• R accepts files of many formats, we will learn importing
files of the following formats:
• Text (.txt)
• CSV (.csv)
• Excel (.xls)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R: Importing Data
• Text files: use read.table() for space separated files,
comma separated files etc..
• CSV files: use read_csv() from readr package (used by
Rstudio interface)
• Excel files: use read_excel() from readxl package (used
by Rstudio interface)
See Rstudio examples to set Working Directory and import
different datasets
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
R: Importing Data
• For more formats (such as SPSS, SAS, STATA files etc…)
you can visit http://cran.rproject.org/doc/manuals/R-
data.pdf , here you get information on how to import
image files as well !
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Data Views
There are several ways to look at a data set:
• First, you can simply look at it entirely by double clicking
on it in the Global Environment, or by using
View(data_name) function
• You can look a specific column by calling it. E.g. data-
name$column_name
• Else, you can look at the first k rows, or the last k rows by
using head(data_name, k) or tail(data_name, k)
respectively
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Data Overviews
You can also use functions to have a quick overview of the
data set you are working with:
• Try to use summary(data_name)
• You can also use str(data_name)
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTERV. Data Manipulations
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited
Filtering/Subsetting
• Use a Logical Operator
• ==, >, <, <=, >=, != are all logical operators.
• Note that the “equals” logical operator is two "==" signs, as one
"= " only is reserved for assignment.
• Result is a Logical variable
• To filter out rows in a dataset, place logic condition(s) in the
dataset’s squared brackets, before the coma
• You can filter using several conditions and separate them with
logical operators “|” (OR) and/or “&” (AND)
• See examples in Rstudio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Binding
• Binding columns: If 2 datasets, a dataset and a vector, or
2 vectors have the same number of values (rows in the
case of datasets), they can be placed together into one
same dataset using cbind()
• This is different from « merging » (see later chapter),
hence there is no row matching system: rows need to be
in the exact same order for the data to make sense.
• See example in Rstudio
• Binding rows: If 2 datasets have the same columns
(order, data types, names), one can be appended under
the other using rbind()
• See example in Rstudio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Transforming
• You can create new columns or modify existing ones by
applying transformations to them
• Transformations can be adding, subtracting,
multiplying, dividing, powering etc..
• But it can also be using functions such as log(), exp()
etc..
• See examples in R studio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Sorting
• In R, you can sort your dataset’s rows based on a
column’s alphabetical order (character variables), or
numerical order (numeric variables)
• You can apply an ascending or descending direction to
this order
• See examples in R studio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTERVI. Joins, Summary
Tables and Data Export
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited
Joins
• Joining consists in combining 2 or more datasets’ rows based
on a common column/field between them
• For a join to happen, 2 datasets need at least one same
column. It matches rows that have identical values in this
column.
• Eg.
• Note: It is not like what the cbind() function does: cbind() fuses datasets by pasting
them one next to the other, regardless of what is in the data
Table 1 Table 2
Column A Column B Column B Column C
A1 B1 B2 C1
A2 B1 B1 C2
A3 B2 B2 C3
Joined Tables
Column A Column B Column C
A1 B1 C2
A2 B1 C2
A3 B2 C1
A3 B2 C3
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Joins
• There are different types of joins :
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Summary Tables
• Contingency tables: Use table(cat_var1,cat_var2)
(where cat_var1 and cat_var2 are categorical
variables) to obtain the observations count for
each combination of these variables’ levels.
• Diverse summary tables: Use data %>%
group_by(cat_var1) %>% summarise() from the
“Dplyr” package to aggregate datasets and obtain
the summary numbers you want.
• See examples in Rstudio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Export Data
• Export data to use outside of R: You can export
your datasets as .csv files using the write.csv()
function.
• Export data for later use in R: You can export your
datasets as R objects called .RDS files using
saveRDS(). You can import them into R using
readRDS(). These execute a lot faster.
• See examples in Rstudio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
CHAPTERVII. Plots
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited
Plots
• Plots (Graphs, Visualisations,..) are very powerful
tools. They allow you to quickly grasp trends and
patterns in data sets, some of which could not be
spotted by analysing summary tables only
• In R, ‘ggplot2’ package gives you endless
possibilities to create visualisations.
• In this video, we focus on qplot() function (from
‘ggplot2’), which can provide high quality graphs
with very little effort.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Plots
With qplot(), we can create:
• Histograms and Density plots to visualise Numerical
variables
• Bar plots to visualise categorical variables
• Box plots to visualise correlations between numerical and
categorical variables
• Dot Plots to visualise correlations between numerical
variables
We can also use color coding to add information to
graphs while keeping them easily interpretable. See
examples in R studio.
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Plots
Finally, you can save your graphs as images.
Simply use the ggsave() function from the ggplot2
package
See examples in Rstudio
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use
or distribution prohibited
Thank you
Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or
distribution prohibited

More Related Content

Similar to Introduction+to+R.pdf

Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAnshika865276
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01shaziabibi5
 
Unit1_Introduction to R.pdf
Unit1_Introduction to R.pdfUnit1_Introduction to R.pdf
Unit1_Introduction to R.pdfMDDidarulAlam15
 
PPT - Introduction to R.pdf
PPT - Introduction to R.pdfPPT - Introduction to R.pdf
PPT - Introduction to R.pdfssuser65af26
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSonaCharles2
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsRanel Padon
 
R Introduction
R IntroductionR Introduction
R Introductionschamber
 
Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programmingJanu Jahnavi
 
Introduction to basic statistics
Introduction to basic statisticsIntroduction to basic statistics
Introduction to basic statisticsIBM
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.pptrajalakshmi5921
 
Android resource
Android resourceAndroid resource
Android resourceKrazy Koder
 

Similar to Introduction+to+R.pdf (20)

Basics.ppt
Basics.pptBasics.ppt
Basics.ppt
 
Advanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.pptAdvanced Data Analytics with R Programming.ppt
Advanced Data Analytics with R Programming.ppt
 
R training
R trainingR training
R training
 
Data structures cs301 power point slides lecture 01
Data structures   cs301 power point slides lecture 01Data structures   cs301 power point slides lecture 01
Data structures cs301 power point slides lecture 01
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
Unit1_Introduction to R.pdf
Unit1_Introduction to R.pdfUnit1_Introduction to R.pdf
Unit1_Introduction to R.pdf
 
Basics R.ppt
Basics R.pptBasics R.ppt
Basics R.ppt
 
PPT - Introduction to R.pdf
PPT - Introduction to R.pdfPPT - Introduction to R.pdf
PPT - Introduction to R.pdf
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Slides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MDSlides on introduction to R by ArinBasu MD
Slides on introduction to R by ArinBasu MD
 
17641.ppt
17641.ppt17641.ppt
17641.ppt
 
Python Programming - VI. Classes and Objects
Python Programming - VI. Classes and ObjectsPython Programming - VI. Classes and Objects
Python Programming - VI. Classes and Objects
 
R tutorial
R tutorialR tutorial
R tutorial
 
Essentials of R
Essentials of REssentials of R
Essentials of R
 
R Introduction
R IntroductionR Introduction
R Introduction
 
Analytics using r programming
Analytics using r programmingAnalytics using r programming
Analytics using r programming
 
Introduction to basic statistics
Introduction to basic statisticsIntroduction to basic statistics
Introduction to basic statistics
 
How to obtain and install R.ppt
How to obtain and install R.pptHow to obtain and install R.ppt
How to obtain and install R.ppt
 
3 jf h-linearequations
3  jf h-linearequations3  jf h-linearequations
3 jf h-linearequations
 
Android resource
Android resourceAndroid resource
Android resource
 

Recently uploaded

Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 

Introduction+to+R.pdf

  • 1. Introduction to R Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 2. CHAPTER I. Course Overview and Preliminary Steps Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 3. Learningobjectives • Know what is R and how it works • Learn basics of working with data in R • Get familiar with basic commands/functions • Learn how to do basic analysis on any dataset and be able to create basic charts Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 4. What is R & Why we use it • It’s a tool : Open-Source, cross platform, free programming language designed to build statistical solutions • Powerful : Gives access to CRAN repository containing over 10,000 packages with pre-defined functions for almost every purpose • Stays Relevant : Constantly being updated by users ( Scientists, Statisticians, Researchers, Students!) • More: Makes beautiful graphs, can create custom functions or modify existing ones, can be integrated into many environments and platforms such as Hadoop etc Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 5. Installing R • Can be downloaded for free from http://www.r-project.org/ • Download the version compatible with your OS • Simple/Standard installation process Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 6. Installing R -Studio • Can be downloaded for free from: • https://www.rstudio.com/products/rstudio/download/ • Download the free version compatible with your OS • R needs to be installed before installing R- Studio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 7. R-Studio UI Write your code here Global Environment- See your datasets here Console - see your code run here See your files, graphs, help documentation and installed packages here Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 8. R Commands • Assignments E.g.: x = 1, or x <- 1 • Functions E.g.: print(“Hello World”) • Computations E.g.: 17 + 3 ; x + 5 • Mix E.g.: y = sqrt(16); y = 15 + 5 • Assignment queries will update objects in your R environment • Queries without assignment, as well as ‘call’ of R objects will either generate an output in the console, or in the plot tab Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 9. CHAPTER II. R Basics: DataTypes Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 10. Variable Assignment in R • A basic construct in programming is "variable" • A variable allows you to store a piece of data (‘datum’, e.g. 6, ‘Hello’, etc.. ) or several pieces of data of a common type, and assign them a unique name • You can then later ‘call’ this variable's name to easily access the value(s) that is/are stored within this variable. Careful, R is case sensitive: The variables ‘x’ and ‘X’ can coexist in R environment and have different values. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 11. Basic data types in R • R works with numerous data types. The most common types are: • Decimals values like 3.5, called 'numeric' • Natural numbers like 3 are called 'integers'. Integers are also numeric • Boolean variables (TRUE or FALSE) are classified as ‘logical’ • Text (or string) values are classified as 'character’ Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 12. Basic data types in R • Categorical variables are called ‘factors’. They have a finite and defined set of values they can take (e.g. eye_color can take have a value contained in {‘blue’, ‘green’, ’brown’, ‘black’}) • Other variables can contain time data such as dates, day of the week, hours, minutes, etc.. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 13. CHAPTER III. R Basics: Data Structures Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 14. R Objects:Vectors • To assign multiple values to a variable, we can use an R object called a ‘vector’ • A vector is a sequence/collection of data elements of the same basic type. Members in a vector are officially called components. For Example: my_vector = c(14,26,38,30) • To access a specific element in the vector, we simply need to call variable_name[i], ‘i’ being the element’s position in the vector. For example: vect[3] would return 38 Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 15. R Objects: Matrices • A matrix is a sequence/collection of data elements of the same basic type arranged in a two-dimensional rectangular layout. • Being a 2-dimensional object, in order to obtain a specific value within the matrix, 2 coordinates needs to be entered. For example: my_matrix[i,j] would return the element on the ith row, in the jth column • my_matrix[i,] would return the entire ith row • my_matrix[,j] would return the entire jth column Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 16. R Objects: Data Frames • A data frame is used for storing data tables. It is a list of vectors of equal length. Unlike matrices, it can gather vectors containing different basic types • Selection of specific elements in data frames works the same way as for matrices. For example: my_dataframe[i,j] would return the element on the ith row, in the jth column Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 17. R Objects: Lists • A list in R allows you to gather a variety of objects under one name (that is, the name of the list) in an ordered way. These objects can be matrices, vectors, data frames, even other lists, etc. It is not even required that these objects are related to each other. • To access the ith object in the list, write my_list[[i]] • If you want to access a variable in the ith object in the list, write my_list[[i]] [variable coordinates]. See examples in R Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 18. CHAPTER IV. Importing Packages and Datasets,Viewing Data Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 19. R: Packages • R Packages are collections of R functions and data sets • Some standard ones come with R installation • Others can be installed in a few clicks in Rstudio, or using install.packages(“package name”) function. You can choose the CRAN Mirror closest to your location, but the default Rstudio is consistently good all over the world. • Some have to be downloaded ( from http://cran.r- project.org/, or through Google and manually installed • Once installed we need to call the package in when needed using “library(“package name”)” Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 20. R: Importing Data • More often than not, data is already available in different formats ready to be imported to R. • R accepts files of many formats, we will learn importing files of the following formats: • Text (.txt) • CSV (.csv) • Excel (.xls) Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 21. R: Importing Data • Text files: use read.table() for space separated files, comma separated files etc.. • CSV files: use read_csv() from readr package (used by Rstudio interface) • Excel files: use read_excel() from readxl package (used by Rstudio interface) See Rstudio examples to set Working Directory and import different datasets Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 22. R: Importing Data • For more formats (such as SPSS, SAS, STATA files etc…) you can visit http://cran.rproject.org/doc/manuals/R- data.pdf , here you get information on how to import image files as well ! Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 23. Data Views There are several ways to look at a data set: • First, you can simply look at it entirely by double clicking on it in the Global Environment, or by using View(data_name) function • You can look a specific column by calling it. E.g. data- name$column_name • Else, you can look at the first k rows, or the last k rows by using head(data_name, k) or tail(data_name, k) respectively Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 24. Data Overviews You can also use functions to have a quick overview of the data set you are working with: • Try to use summary(data_name) • You can also use str(data_name) Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 25. CHAPTERV. Data Manipulations Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 26. Filtering/Subsetting • Use a Logical Operator • ==, >, <, <=, >=, != are all logical operators. • Note that the “equals” logical operator is two "==" signs, as one "= " only is reserved for assignment. • Result is a Logical variable • To filter out rows in a dataset, place logic condition(s) in the dataset’s squared brackets, before the coma • You can filter using several conditions and separate them with logical operators “|” (OR) and/or “&” (AND) • See examples in Rstudio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 27. Binding • Binding columns: If 2 datasets, a dataset and a vector, or 2 vectors have the same number of values (rows in the case of datasets), they can be placed together into one same dataset using cbind() • This is different from « merging » (see later chapter), hence there is no row matching system: rows need to be in the exact same order for the data to make sense. • See example in Rstudio • Binding rows: If 2 datasets have the same columns (order, data types, names), one can be appended under the other using rbind() • See example in Rstudio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 28. Transforming • You can create new columns or modify existing ones by applying transformations to them • Transformations can be adding, subtracting, multiplying, dividing, powering etc.. • But it can also be using functions such as log(), exp() etc.. • See examples in R studio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 29. Sorting • In R, you can sort your dataset’s rows based on a column’s alphabetical order (character variables), or numerical order (numeric variables) • You can apply an ascending or descending direction to this order • See examples in R studio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 30. CHAPTERVI. Joins, Summary Tables and Data Export Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 31. Joins • Joining consists in combining 2 or more datasets’ rows based on a common column/field between them • For a join to happen, 2 datasets need at least one same column. It matches rows that have identical values in this column. • Eg. • Note: It is not like what the cbind() function does: cbind() fuses datasets by pasting them one next to the other, regardless of what is in the data Table 1 Table 2 Column A Column B Column B Column C A1 B1 B2 C1 A2 B1 B1 C2 A3 B2 B2 C3 Joined Tables Column A Column B Column C A1 B1 C2 A2 B1 C2 A3 B2 C1 A3 B2 C3 Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 32. Joins • There are different types of joins : Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 33. Summary Tables • Contingency tables: Use table(cat_var1,cat_var2) (where cat_var1 and cat_var2 are categorical variables) to obtain the observations count for each combination of these variables’ levels. • Diverse summary tables: Use data %>% group_by(cat_var1) %>% summarise() from the “Dplyr” package to aggregate datasets and obtain the summary numbers you want. • See examples in Rstudio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 34. Export Data • Export data to use outside of R: You can export your datasets as .csv files using the write.csv() function. • Export data for later use in R: You can export your datasets as R objects called .RDS files using saveRDS(). You can import them into R using readRDS(). These execute a lot faster. • See examples in Rstudio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 35. CHAPTERVII. Plots Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 36. Plots • Plots (Graphs, Visualisations,..) are very powerful tools. They allow you to quickly grasp trends and patterns in data sets, some of which could not be spotted by analysing summary tables only • In R, ‘ggplot2’ package gives you endless possibilities to create visualisations. • In this video, we focus on qplot() function (from ‘ggplot2’), which can provide high quality graphs with very little effort. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 37. Plots With qplot(), we can create: • Histograms and Density plots to visualise Numerical variables • Bar plots to visualise categorical variables • Box plots to visualise correlations between numerical and categorical variables • Dot Plots to visualise correlations between numerical variables We can also use color coding to add information to graphs while keeping them easily interpretable. See examples in R studio. Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 38. Plots Finally, you can save your graphs as images. Simply use the ggsave() function from the ggplot2 package See examples in Rstudio Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited
  • 39. Thank you Proprietary content. ©Great Learning. All Rights Reserved. Unauthorized use or distribution prohibited