SlideShare uma empresa Scribd logo
1 de 48
Getting Started With
R
SECTION 1
Introduction to R
2
What is R?
• R is a programming language and environment for statistical computing and
graphics
• It has a wide variety of statistical & graphical techniques built in which makes it
one of the most popular languages used by statisticians, data analysts, researchers
and marketers to retrieve, clean, analyze, visualize and present data
• R is Free and Open Source software which means the source code is publicly
accessible and can be modified as users or other developers see fit
• R compiles and runs on a wide variety of UNIX platforms, Windows and MacOS
3
R Environment & Community
• Almost all functionality is done through built in functions. Basic and advanced
functions are available by default
• R is built using packages. There are approximately 13,393 packages
• All datasets created in the session remain in Memory
• R commands are Case Sensitive
• R Community is a group of developers who maintain R and guide its evolution
• Few of the contributors are: R-bloggers, Revolution Analytics Blog, R-statistics
and Data Mining Blog
• There are news, blogs, forums, research on new packages or advancements, on their
websites
• They enable you to connect with other users and you can also contribute to these
communities to both help others and learn the material and strengthen your own
understanding 4
SECTION 2
Installing R
5
Installing R
R-CRAN:
• The Comprehensive R Archive Network - A network of global web servers storing
identical, up-to-date versions of code and documentation for R
• It is the main repository for R packages. Currently, it features 9763 available packages
To install R:
• Go to http://cran.r-project.org/
• Download the setup
• Run the setup
6
The R-User Interface
7
The R console
− Type in the command
− Press enter
− See the output
RStudio
• Integrated Development Environment (IDE) is a software which provides
programmer with an interface combined with all the required tools at hand
• RStudio is a free and open source IDE for R
• It is more user-friendly than using R directly since it keeps track of your script
file, console, plots, and history, all in one place
• It has an organized layout and several extra options
• The usual RStudio screen has four windows:
− Console
− Workspace and history
− Files, plots, packages and help
− The R script(s) and data view
To download RStudio go to https://www.RStudio.com/and follow the steps.
8
RStudio - User Interface
The console is where you can type
commands and see output
The workspace tab shows all the
active objects. The history tab
shows a list of commands used so
far
The files tab shows all the files and
folders in your default workspace as if
you were on a PC/Mac window
The plots tab will show all your graphs.
The packages tab will list a series of
packages or add-ons needed to run
certain processes. For additional info
see the help tab 9
The script is where you can write a
bunch of programs, execute them
and can save it for later use
SECTION 3
Write Your First R Program
10
Working with Directories
• A working directory is a default location on your hard disk where R looks
without further instruction for any files you ask it to retrieve or write. It is
important to select a location that is easy to find and remember, so you can access
your files when you need them
• In order to check what your R current working directory is type:
getwd()
• If the directory it returns is not the directory you would like to use, then simply use
the following function to set your working directory:
setwd(‘PATH’)
replace PATH with the directory path that you would like to use
• Use getwd() again to verify that the change took place
11
Working with Directories
• Steps to set working directory through R tools:
– Go to the File menu
– Select Change Working Directory
– Select the appropriate folder/directory
• In case of Windows, R’s default working directory is:
"C:/Users/User Name/Documents"
You have the option to set the working directory at any time. Do this when you
want to access files in a new location, such as when you are working on multiple
projects at the same time or at the start of a new project.* 12
Create and Save Your Script
In your R console,
• Open a new script. File > New script
The R script
− Write commands
− Execute or run the command using
F5 key or Ctrl+R.
− See the output
• You can run a single command,
bunch of commands by highlighting
those, or the entire file at once.
• Save the file at a desired location
from the ‘File’ menu. It helps to save
the script for later use.
13
Write a Program
Create two objects ‘x’ and ‘y’ and perform mathematical operations on them.
• The operators ‘<-’ and ‘=‘ are used for assigning values to an object
• To see the value of the object, just type the name of the object and press the enter
key
# Create an object x and assign a value 10
x<-10
x
[1] 10
# Type x and press Enter key
• Comments can be added to keep a record of what you did and why, by preceding all
comments with the # symbol
14
Write a Program
y<-7
y
[1] 7
# Create another object y and assign a value 7
x + y
[1] 17
# Add the values of the objects x and y
x * y
[1] 70
# Multiply x by y
R is case sensitive i.e. it treats ‘a’ and ‘A’ as completely different objects.
* 15
R Workspace
• Objects that you create during an R session are held in memory, the collection of
objects that you currently have is called the workspace
• This workspace is not saved on disk unless you tell R to do so. This means that your
objects are lost when you close R without saving them or worse when R or your
system crashes during a session. If it is saved then all the previously saved objects will
be available when you start the R next time
Some basic commands for using the command history:
16
history() To display all previous commands
history(max.show=25) To display last 25 commands
savehistory(file="myfile") To save your command history to a file.
Default is ".Rhistory“
myfile is saved in My Documents
loadhistory(file="myfile") To recall your command history.
Default is ".Rhistory"
Help in R
• R has a comprehensive built-in help system consisting of a documentation for
packages and functions. Any of the following commands can be used to access help
in R
• If you know the topic but not the exact function:
• If you know the exact function:
help.search("regression")
OR
??"regression"
help(functionname)
help(mean)
OR
?functionname
?mean
17
SECTION 4
Packages in R
18
R Package
• A Package is a collection of R functions with comprehensive documents
• A core set of packages is included with the installation of R. Additional packages are
required for specific requirements
• These packages can be downloaded from various repositories like Comprehensive R
Archive Network, or from bioconductor, github, R Forge and many more
• Packages extend the functionality of R by enabling additional visual capabilities,
statistical methods, and discipline-specific functions, just to name a few.
A Package includes:
R functions, Data Example, Help Files, Namespace
and Description.
19
How to Install and Use a Package ?
To install a package
Type the following command in R
console:
install.packages(“pkg ”)
pkg is the name of package
whose current version should
be downloaded from the
repositories.
Some packages have been removed from CRAN but they can be downloaded from
R archives. Link for archive: http://cran.r-project.org/src/contrib/Archive/
Download the most recently modified version of required package from the link
above.
* 20
To use a package
Invoke the following
command to load the
package into the ongoing
session:
library(<pkg>)
SECTION 5
Data Types in R
21
Data Types in R
Numeric
Integer
Character
Factor
Logical
Contains decimal as well as whole numbers
Contains only whole numbers
Holds character strings
A vector that can contain only predefined values, and is
used to store categorical data.
Can only take on two values, TRUE or FALSE.
22
Data Types in R
Vector
Matrix
Array
Data frame
List
1 dimension
2 dimensions
2 or more
dimensions
2 dimensions
Sequence of data elements of the same basic
type.
Like a Vector but additionally contains the
dimension attribute.
Hold multidimensional data. Matrices are a
special case of two-dimensional arrays.
Table-like data object allowing different data
types for different columns.
Collection of data objects, each element of a list is a data object.
23
Numeric
x<-4.5
x
[1] 4.5
y<-3567
y
[1] 3567
class(x)
[1] "numeric"
class(y)
[1] "numeric"
class() function is used to check the data
type of an object.
Numeric type object can store decimal as well as whole numbers.
Create two numeric objects x and y and assign values 4.5 and 3567 respectively.
24
Numeric
R shows class of an integer object x as numeric. To convert it to an integer format, use
as.integer() function.
x<-as.integer(x)
x
[1] 4
class(x)
[1] "integer"
new value of x is 4 after converting it into
integer
25
Integer
To create an integer variable in R use as.integer() function.
f<-as.integer(22.5)
f
[1] 22
class(f)
[1] "integer"
x=8
class(x)
[1] "numeric"
The default class of an integer is a numeric
class
26
Character
A character object is used to represent strings. A string is specified by using quotes
(both single and double quotes will work). To convert any object into a character type,
use as.character() function.
z<-"Welcome to The World of R"
z
[1] "Welcome to The World of R"
x<-"4.5"
x
[1] "4.5"
class(z)
[1] "character"
class(x)
[1] "character"
Any value enclosed in quotes is stored as a
character object
27
x<-c("high", "medium", "low", "low", "medium", "high", "high", "high",
"medium", "low","low")
Factor
Factor objects are used to categorise data and can store both strings and integers.
# Create an object x
is.factor(x)
[1] FALSE
# Check whether object x is a factor or character
is.character(x)
[1] TRUE
c() combines data of different types
is.factor() function returns True or False after checking whether the object
is of type factor or not
is.character() function returns True or False after checking
whether the object is of type character or not
28
levels(x)
[1] "high" "low" "medium"
Factor
Factor object x has 11 elements and 3 levels. By default the levels
are sorted alphabetically
x<-factor(x)
x
[1] high medium low low medium high high high
[9] medium low low
Levels: high low medium
A factor is a categorical variable that can take only one of a fixed, finite set of
possibilities. Those possible categories are the levels. Levels are the unique data
values. Above output tells how many levels are there in x. Alternatively, we can use
levels() function to check levels.
Create a factor object using factor() function
29
Logical
x<-4.5
is.integer(x)
[1] FALSE
is.integer() function checks whether the object is
integer or not.
# Create an object x and assign a value 4.5
# Create two numeric objects y and z
# Check whether y is greater than z or not
y<-4
z<-7
result<-y>z
result
[1] FALSE
With this kind of statement, you are asking R
to evaluate the logical question “Is it true that y is
greater than z?”
The object storing the answer of above question is of
type logical
You can check the class of the object using class()
Logical type objects can only take 2 values i.e. TRUE or FALSE.
30
Vector
a <- c(1,2,5.3,6,-2,4)
a
[1] 1.0 2.0 5.3 6.0 -2.0 4.0
Create three vectors: numeric, character and logical. All the elements of a vector must be
of the same type. To convert any object into a vector type, use as.vector() function.
# Numeric vector
b <- c("one","two","three")
b
[1] "one" "two" "three"
# Character vector
c<-c(4,24,6,4, 2,7)
l<-c>5
l
[1] FALSE TRUE TRUE FALSE FALSE TRUE
# Logical vector
31
x<-matrix(c(2, 3, 4, 5, 6, 7),nrow=3,ncol=2,byrow=TRUE)
x
[,1] [,2]
[1,] 2 3
[2,] 4 5
[3,] 6 7
x<-matrix(c(2, 3, 4, 5, 6, 7),nrow=3,ncol=2)
x
[,1] [,2]
[1,] 2 5
[2,] 3 6
[3,] 4 7
Matrices
byrow=TRUE fills the matrix row-wise
matrix() function is used to create a matrix.
nrow= and ncol= is used to specify the dimension of the matrix
Note that the matrix is filled column-wise. To fill it row-wise see
the next command
Matrix is a two dimensional data structure. It is similar to vectors but additionally
contains the dimension attribute. To convert any object into a matrix type, use
as.matrix() function.
# Create a matrix with 3 rows and 2 columns.
32
Arrays
a<-array(1:24,dim=c(3,4,2))
a
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 4 7 10
[2,] 2 5 8 11
[3,] 3 6 9 12
, , 2
[,1] [,2] [,3] [,4]
[1,] 13 16 19 22
[2,] 14 17 20 23
[3,] 15 18 21 24
array() is used to create array in R, where
you give data as the first argument
and a vector with the sizes of the dimensions
as the second argument.
dim= takes dimensions of the array.
Note: Although the rows are given as the
first dimension, the tables are filled column-
wise. So, for arrays, R fills the columns, then
the rows, and then the rest
Arrays hold multidimensional rectangular data i.e. each row is the same length, and
likewise for each column and other dimensions
# Create an array with four columns, three rows and two “tables”.
33
Data Frames
x<-c(12,23,45)
y<-c(13,21,6)
z<-c("a","b","c")
data<-data.frame(x,y,z)
data
x y z
1 12 13 a
2 23 21 b
3 45 6 c
creates vectors x, y, z
data.frame() function
combines them in a table.
object data is a dataframe
containing three vectors x, y, z
Data frame is a two-dimensional array like structure. It is a list of vectors of equal
length. Data frames are the primary data structure in R. To convert any object into a
data frame type, use as.data.frame() function.
# Create a data frame from different vectors
Data.frames are distinct from matrices because they can include heterogeneous
data types among columns/variables.
* 34
Lists
n=c(2, 3, 5)
s=c("aa", "bb", "cc", "dd", "ee")
x=list(n, s, 3)
[[1]]
[1] 2 3 5
[[2]]
[1] "aa" "bb" "cc" "dd" "ee"
[[3]]
[1] 3
list() is used to create lists.
List is a data structure having components of mixed data types. To convert any object
into a list type, use as.list() function.
# Create a list of 2 vectors and a numeric value
35
We retrieve a list slice by enclosing the index of the vector in a square bracket "[]" operator,
for eg: x[2]
SECTION 6
String Functions in R
36
Concatenating Strings - paste()
paste() converts its arguments to character strings and concatenates (pastes) them
to form one or several character strings.
a<-"Hello"
b<-'How'
c<-"are you? "
paste(a,b,c,sep="-")
[1] "Hello-How-are you? "
paste("y",1:4,sep=".")
[1] "y.1" "y.2" "y.3" "y.4"
sep= takes a character string that is used as
a seperator. The default separator is a blank
space (sep=" ")
single character “y” is pasted with the
sequence 1:4 and separator sep="."
37
Extracting and Replacing - substring()
substring("WELCOME TO THE WORLD OF R",12,14)
[1] "THE"
substring() extracts or replaces substrings in a character vector.
# Extract ‘THE’
Characters from 12th to 14th position are
extracted
38
Location<-c("Mumbai","Delhi","Mumbai","Kolkata","Delhi")
substring(Location,1,3)
[1] "Mum" "Del" "Mum" "Kol" "Del"
# Extract first 3 letters
string=c("G1:E001","G2:E002","G3:E003")
substring(string,3)<-" "
[1] "G1 E001" "G2 E002" "G3 E003"
# Replacing ‘:’ with ‘ ’
Replacing Substrings – sub() & gsub()
string<-"She is a data scientist. She works with an MNC"
sub("She","Sharon",string)
[1] "Sharon is a data scientist. She works with an MNC"
• sub() replaces the first instance of a substring
sub() finds the first instance of the ‘She’
within string and replaces it with ‘Sharon’
39
• gsub() replaces all instances of a substring. The g in gsub() stands for global.
gsub("She","Sharon",string)
[1] "Sharon is a data scientist. Sharon works with an MNC"
gsub() does the same thing as sub(), but
it replaces all instances of the substring(a
global replace), not just the first
SECTION 7
Conditional Statements in R
40
Conditional Statements
• Conditional Statements are useful in decision making when we want to
execute a code only if a certain condition is satisfied
• Condition is referred as a logical expression, when evaluated returns
either True or False and depending on that the statements get executed
• The simplest form is if statement. else and else if statements can also be
used depending on the number of conditions to be checked
41
if Statement
If the condition is TRUE, the statement gets executed. But if it's FALSE, nothing
happens.
x<-5
if(x > 0){
print("Positive number")
}
[1] "Positive number"
x<-"Hello there"
if(is.character(x)) {
print(x)
}
[1] "Hello there"
# Check if x is a positive number or not
# Print the value of the object if it is of a character type
Comparison operator ‘>’ is used in condition.
Here, x >0 returns TRUE; hence the print
statement is executed
is.character() returns TRUE or FALSE
depending on whether the argument is of
character type or not.
Here x is a character, hence the condition
returns TRUE and “Hello there” is printed
42
x<- -5
if(x > 0){
print("Positive number")
} else {
print("Negative number")
}
[1] "Negative number"
if..else Statement
This condition evaluates to
false; hence it will skip the
body of if and the body of
else will be executed.
if..else Statements are used when you want to execute one block of code when a
condition is true, and another block of code when it is false.
There is an easier way to use if..else statement specifically for vectors. You can
use ifelse() function instead.
*
# Check if x is a positive number or not and print the status
43
ifelse()
ifelse() Function is a vector equivalent form of the if..else statement
a<-c(6,1,5,14)
ifelse(a%%2==0,"even","odd")
[1] "even" "odd" "odd" "even"
ifelse() function can take a vector as an input
and results into a vector. If the condition is TRUE
it returns x else y
Here, a is a vector, put in a condition to check
and print even or odd depending on result.
%% operator gives the remainder of the first
vector with the second
44
SECTION 8
Loops in R
45
for Loop
Syntax:
for (Loop_Variable in Sequence) {
Statement 1
Statement 2
....
}
Loop_Variable sets a new value for each
iteration of the loop.
Sequence is a vector assigned to a
Loop_Variable
Statement 1,
Statement 2,…
Body of for consisting of
block of program
statements contained within
curly braces 46
Loops are used when a block of codes needs to be executed multiple times (called
iteration)
for Loop
• In for loop, the number of times the loop should be executed is typically defined
in the initialisation part
• Loop variable is created when the for statement runs
• Loop variable is assigned the next element in the vector with each iteration and then
the statements of the body are executed
for ( i in 1:4) {
print(i)
}
[1] 1
[1] 2
[1] 3
[1] 4
# Print numbers 1 to 4
47
THANK YOU!
48

Mais conteúdo relacionado

Mais procurados

Data visualization using R
Data visualization using RData visualization using R
Data visualization using RUmmiya Mohammedi
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in RRupak Roy
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-exportFAO
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming FundamentalsRagia Ibrahim
 
R programming language
R programming languageR programming language
R programming languageKeerti Verma
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using RVictoria López
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to RstudioOlga Scrivner
 
R programming slides
R  programming slidesR  programming slides
R programming slidesPankaj Saini
 
R language tutorial
R language tutorialR language tutorial
R language tutorialDavid Chiu
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with RFAO
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R StudioRupak Roy
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With REdureka!
 

Mais procurados (20)

Data visualization using R
Data visualization using RData visualization using R
Data visualization using R
 
R studio
R studio R studio
R studio
 
Data Types and Structures in R
Data Types and Structures in RData Types and Structures in R
Data Types and Structures in R
 
R data-import, data-export
R data-import, data-exportR data-import, data-export
R data-import, data-export
 
Data Visualization With R
Data Visualization With RData Visualization With R
Data Visualization With R
 
R programming Fundamentals
R programming  FundamentalsR programming  Fundamentals
R programming Fundamentals
 
R programming language
R programming languageR programming language
R programming language
 
Unit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptxUnit 1 - R Programming (Part 2).pptx
Unit 1 - R Programming (Part 2).pptx
 
R programming
R programmingR programming
R programming
 
Introduction to statistical software R
Introduction to statistical software RIntroduction to statistical software R
Introduction to statistical software R
 
Introduction to data analysis using R
Introduction to data analysis using RIntroduction to data analysis using R
Introduction to data analysis using R
 
Introduction to Rstudio
Introduction to RstudioIntroduction to Rstudio
Introduction to Rstudio
 
R programming slides
R  programming slidesR  programming slides
R programming slides
 
R language tutorial
R language tutorialR language tutorial
R language tutorial
 
Data preprocessing
Data preprocessingData preprocessing
Data preprocessing
 
Python ppt
Python pptPython ppt
Python ppt
 
8. R Graphics with R
8. R Graphics with R8. R Graphics with R
8. R Graphics with R
 
Introduction to R and R Studio
Introduction to R and R StudioIntroduction to R and R Studio
Introduction to R and R Studio
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Linear Regression With R
Linear Regression With RLinear Regression With R
Linear Regression With R
 

Semelhante a Getting Started with R

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
 
BUSINESS ANALYTICS WITH R SOFTWARE DIAST
BUSINESS ANALYTICS WITH R SOFTWARE DIASTBUSINESS ANALYTICS WITH R SOFTWARE DIAST
BUSINESS ANALYTICS WITH R SOFTWARE DIASTHaritikaChhatwal1
 
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
 
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
 
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
 
R - Get Started I - Sanaitics
R - Get Started I - SanaiticsR - Get Started I - Sanaitics
R - Get Started I - SanaiticsVijith Nair
 
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitrWiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitrAnn Loraine
 
Introduction to r
Introduction to rIntroduction to r
Introduction to rgslicraf
 
Intro to data science module 1 r
Intro to data science module 1 rIntro to data science module 1 r
Intro to data science module 1 ramuletc
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiUnmesh Baile
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programmingNimrita Koul
 

Semelhante a Getting Started with R (20)

R Intro
R IntroR Intro
R Intro
 
R training
R trainingR training
R training
 
Unit1_Introduction to R.pdf
Unit1_Introduction to R.pdfUnit1_Introduction to R.pdf
Unit1_Introduction to R.pdf
 
PPT - Introduction to R.pdf
PPT - Introduction to R.pdfPPT - Introduction to R.pdf
PPT - Introduction to R.pdf
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 
Introduction to R software, by Leire ibaibarriaga
Introduction to R software, by Leire ibaibarriaga Introduction to R software, by Leire ibaibarriaga
Introduction to R software, by Leire ibaibarriaga
 
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي   R program د.هديل القفيديمحاضرة برنامج التحليل الكمي   R program د.هديل القفيدي
محاضرة برنامج التحليل الكمي R program د.هديل القفيدي
 
BUSINESS ANALYTICS WITH R SOFTWARE DIAST
BUSINESS ANALYTICS WITH R SOFTWARE DIASTBUSINESS ANALYTICS WITH R SOFTWARE DIAST
BUSINESS ANALYTICS WITH R SOFTWARE DIAST
 
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
 
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
 
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
 
R - Get Started I - Sanaitics
R - Get Started I - SanaiticsR - Get Started I - Sanaitics
R - Get Started I - Sanaitics
 
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitrWiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
WiNGS 2014 Workshop 2 R, RStudio, and reproducible research with knitr
 
Introduction to r
Introduction to rIntroduction to r
Introduction to r
 
R- Introduction
R- IntroductionR- Introduction
R- Introduction
 
Intro to data science module 1 r
Intro to data science module 1 rIntro to data science module 1 r
Intro to data science module 1 r
 
Best corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbaiBest corporate-r-programming-training-in-mumbai
Best corporate-r-programming-training-in-mumbai
 
Workshop presentation hands on r programming
Workshop presentation hands on r programmingWorkshop presentation hands on r programming
Workshop presentation hands on r programming
 

Mais de Sankhya_Analytics

Mais de Sankhya_Analytics (8)

Getting Started with Python
Getting Started with PythonGetting Started with Python
Getting Started with Python
 
Data Management in Python
Data Management in PythonData Management in Python
Data Management in Python
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
Getting Started with MySQL II
Getting Started with MySQL IIGetting Started with MySQL II
Getting Started with MySQL II
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Basic Analysis using R
Basic Analysis using RBasic Analysis using R
Basic Analysis using R
 
R Get Started II
R Get Started IIR Get Started II
R Get Started II
 
R Get Started I
R Get Started IR Get Started I
R Get Started I
 

Último

Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Seán Kennedy
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degreeyuu sss
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...Boston Institute of Analytics
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理e4aez8ss
 
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGILLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGIThomas Poetter
 
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024Timothy Spann
 
Conf42-LLM_Adding Generative AI to Real-Time Streaming Pipelines
Conf42-LLM_Adding Generative AI to Real-Time Streaming PipelinesConf42-LLM_Adding Generative AI to Real-Time Streaming Pipelines
Conf42-LLM_Adding Generative AI to Real-Time Streaming PipelinesTimothy Spann
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxMike Bennett
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFAAndrei Kaleshka
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 217djon017
 
Vision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptxVision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptxellehsormae
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectBoston Institute of Analytics
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)jennyeacort
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfJohn Sterrett
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Seán Kennedy
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档208367051
 

Último (20)

Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...Student profile product demonstration on grades, ability, well-being and mind...
Student profile product demonstration on grades, ability, well-being and mind...
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
办美国阿肯色大学小石城分校毕业证成绩单pdf电子版制作修改#真实留信入库#永久存档#真实可查#diploma#degree
 
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
NLP Data Science Project Presentation:Predicting Heart Disease with NLP Data ...
 
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
科罗拉多大学波尔得分校毕业证学位证成绩单-可办理
 
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGILLMs, LMMs, their Improvement Suggestions and the Path towards AGI
LLMs, LMMs, their Improvement Suggestions and the Path towards AGI
 
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
April 2024 - NLIT Cloudera Real-Time LLM Streaming 2024
 
Conf42-LLM_Adding Generative AI to Real-Time Streaming Pipelines
Conf42-LLM_Adding Generative AI to Real-Time Streaming PipelinesConf42-LLM_Adding Generative AI to Real-Time Streaming Pipelines
Conf42-LLM_Adding Generative AI to Real-Time Streaming Pipelines
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
Semantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptxSemantic Shed - Squashing and Squeezing.pptx
Semantic Shed - Squashing and Squeezing.pptx
 
How we prevented account sharing with MFA
How we prevented account sharing with MFAHow we prevented account sharing with MFA
How we prevented account sharing with MFA
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2Easter Eggs From Star Wars and in cars 1 and 2
Easter Eggs From Star Wars and in cars 1 and 2
 
Vision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptxVision, Mission, Goals and Objectives ppt..pptx
Vision, Mission, Goals and Objectives ppt..pptx
 
Heart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis ProjectHeart Disease Classification Report: A Data Analysis Project
Heart Disease Classification Report: A Data Analysis Project
 
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
Call Us ➥97111√47426🤳Call Girls in Aerocity (Delhi NCR)
 
DBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdfDBA Basics: Getting Started with Performance Tuning.pdf
DBA Basics: Getting Started with Performance Tuning.pdf
 
Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...Student Profile Sample report on improving academic performance by uniting gr...
Student Profile Sample report on improving academic performance by uniting gr...
 
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
原版1:1定制南十字星大学毕业证(SCU毕业证)#文凭成绩单#真实留信学历认证永久存档
 

Getting Started with R

  • 3. What is R? • R is a programming language and environment for statistical computing and graphics • It has a wide variety of statistical & graphical techniques built in which makes it one of the most popular languages used by statisticians, data analysts, researchers and marketers to retrieve, clean, analyze, visualize and present data • R is Free and Open Source software which means the source code is publicly accessible and can be modified as users or other developers see fit • R compiles and runs on a wide variety of UNIX platforms, Windows and MacOS 3
  • 4. R Environment & Community • Almost all functionality is done through built in functions. Basic and advanced functions are available by default • R is built using packages. There are approximately 13,393 packages • All datasets created in the session remain in Memory • R commands are Case Sensitive • R Community is a group of developers who maintain R and guide its evolution • Few of the contributors are: R-bloggers, Revolution Analytics Blog, R-statistics and Data Mining Blog • There are news, blogs, forums, research on new packages or advancements, on their websites • They enable you to connect with other users and you can also contribute to these communities to both help others and learn the material and strengthen your own understanding 4
  • 6. Installing R R-CRAN: • The Comprehensive R Archive Network - A network of global web servers storing identical, up-to-date versions of code and documentation for R • It is the main repository for R packages. Currently, it features 9763 available packages To install R: • Go to http://cran.r-project.org/ • Download the setup • Run the setup 6
  • 7. The R-User Interface 7 The R console − Type in the command − Press enter − See the output
  • 8. RStudio • Integrated Development Environment (IDE) is a software which provides programmer with an interface combined with all the required tools at hand • RStudio is a free and open source IDE for R • It is more user-friendly than using R directly since it keeps track of your script file, console, plots, and history, all in one place • It has an organized layout and several extra options • The usual RStudio screen has four windows: − Console − Workspace and history − Files, plots, packages and help − The R script(s) and data view To download RStudio go to https://www.RStudio.com/and follow the steps. 8
  • 9. RStudio - User Interface The console is where you can type commands and see output The workspace tab shows all the active objects. The history tab shows a list of commands used so far The files tab shows all the files and folders in your default workspace as if you were on a PC/Mac window The plots tab will show all your graphs. The packages tab will list a series of packages or add-ons needed to run certain processes. For additional info see the help tab 9 The script is where you can write a bunch of programs, execute them and can save it for later use
  • 10. SECTION 3 Write Your First R Program 10
  • 11. Working with Directories • A working directory is a default location on your hard disk where R looks without further instruction for any files you ask it to retrieve or write. It is important to select a location that is easy to find and remember, so you can access your files when you need them • In order to check what your R current working directory is type: getwd() • If the directory it returns is not the directory you would like to use, then simply use the following function to set your working directory: setwd(‘PATH’) replace PATH with the directory path that you would like to use • Use getwd() again to verify that the change took place 11
  • 12. Working with Directories • Steps to set working directory through R tools: – Go to the File menu – Select Change Working Directory – Select the appropriate folder/directory • In case of Windows, R’s default working directory is: "C:/Users/User Name/Documents" You have the option to set the working directory at any time. Do this when you want to access files in a new location, such as when you are working on multiple projects at the same time or at the start of a new project.* 12
  • 13. Create and Save Your Script In your R console, • Open a new script. File > New script The R script − Write commands − Execute or run the command using F5 key or Ctrl+R. − See the output • You can run a single command, bunch of commands by highlighting those, or the entire file at once. • Save the file at a desired location from the ‘File’ menu. It helps to save the script for later use. 13
  • 14. Write a Program Create two objects ‘x’ and ‘y’ and perform mathematical operations on them. • The operators ‘<-’ and ‘=‘ are used for assigning values to an object • To see the value of the object, just type the name of the object and press the enter key # Create an object x and assign a value 10 x<-10 x [1] 10 # Type x and press Enter key • Comments can be added to keep a record of what you did and why, by preceding all comments with the # symbol 14
  • 15. Write a Program y<-7 y [1] 7 # Create another object y and assign a value 7 x + y [1] 17 # Add the values of the objects x and y x * y [1] 70 # Multiply x by y R is case sensitive i.e. it treats ‘a’ and ‘A’ as completely different objects. * 15
  • 16. R Workspace • Objects that you create during an R session are held in memory, the collection of objects that you currently have is called the workspace • This workspace is not saved on disk unless you tell R to do so. This means that your objects are lost when you close R without saving them or worse when R or your system crashes during a session. If it is saved then all the previously saved objects will be available when you start the R next time Some basic commands for using the command history: 16 history() To display all previous commands history(max.show=25) To display last 25 commands savehistory(file="myfile") To save your command history to a file. Default is ".Rhistory“ myfile is saved in My Documents loadhistory(file="myfile") To recall your command history. Default is ".Rhistory"
  • 17. Help in R • R has a comprehensive built-in help system consisting of a documentation for packages and functions. Any of the following commands can be used to access help in R • If you know the topic but not the exact function: • If you know the exact function: help.search("regression") OR ??"regression" help(functionname) help(mean) OR ?functionname ?mean 17
  • 19. R Package • A Package is a collection of R functions with comprehensive documents • A core set of packages is included with the installation of R. Additional packages are required for specific requirements • These packages can be downloaded from various repositories like Comprehensive R Archive Network, or from bioconductor, github, R Forge and many more • Packages extend the functionality of R by enabling additional visual capabilities, statistical methods, and discipline-specific functions, just to name a few. A Package includes: R functions, Data Example, Help Files, Namespace and Description. 19
  • 20. How to Install and Use a Package ? To install a package Type the following command in R console: install.packages(“pkg ”) pkg is the name of package whose current version should be downloaded from the repositories. Some packages have been removed from CRAN but they can be downloaded from R archives. Link for archive: http://cran.r-project.org/src/contrib/Archive/ Download the most recently modified version of required package from the link above. * 20 To use a package Invoke the following command to load the package into the ongoing session: library(<pkg>)
  • 22. Data Types in R Numeric Integer Character Factor Logical Contains decimal as well as whole numbers Contains only whole numbers Holds character strings A vector that can contain only predefined values, and is used to store categorical data. Can only take on two values, TRUE or FALSE. 22
  • 23. Data Types in R Vector Matrix Array Data frame List 1 dimension 2 dimensions 2 or more dimensions 2 dimensions Sequence of data elements of the same basic type. Like a Vector but additionally contains the dimension attribute. Hold multidimensional data. Matrices are a special case of two-dimensional arrays. Table-like data object allowing different data types for different columns. Collection of data objects, each element of a list is a data object. 23
  • 24. Numeric x<-4.5 x [1] 4.5 y<-3567 y [1] 3567 class(x) [1] "numeric" class(y) [1] "numeric" class() function is used to check the data type of an object. Numeric type object can store decimal as well as whole numbers. Create two numeric objects x and y and assign values 4.5 and 3567 respectively. 24
  • 25. Numeric R shows class of an integer object x as numeric. To convert it to an integer format, use as.integer() function. x<-as.integer(x) x [1] 4 class(x) [1] "integer" new value of x is 4 after converting it into integer 25
  • 26. Integer To create an integer variable in R use as.integer() function. f<-as.integer(22.5) f [1] 22 class(f) [1] "integer" x=8 class(x) [1] "numeric" The default class of an integer is a numeric class 26
  • 27. Character A character object is used to represent strings. A string is specified by using quotes (both single and double quotes will work). To convert any object into a character type, use as.character() function. z<-"Welcome to The World of R" z [1] "Welcome to The World of R" x<-"4.5" x [1] "4.5" class(z) [1] "character" class(x) [1] "character" Any value enclosed in quotes is stored as a character object 27
  • 28. x<-c("high", "medium", "low", "low", "medium", "high", "high", "high", "medium", "low","low") Factor Factor objects are used to categorise data and can store both strings and integers. # Create an object x is.factor(x) [1] FALSE # Check whether object x is a factor or character is.character(x) [1] TRUE c() combines data of different types is.factor() function returns True or False after checking whether the object is of type factor or not is.character() function returns True or False after checking whether the object is of type character or not 28
  • 29. levels(x) [1] "high" "low" "medium" Factor Factor object x has 11 elements and 3 levels. By default the levels are sorted alphabetically x<-factor(x) x [1] high medium low low medium high high high [9] medium low low Levels: high low medium A factor is a categorical variable that can take only one of a fixed, finite set of possibilities. Those possible categories are the levels. Levels are the unique data values. Above output tells how many levels are there in x. Alternatively, we can use levels() function to check levels. Create a factor object using factor() function 29
  • 30. Logical x<-4.5 is.integer(x) [1] FALSE is.integer() function checks whether the object is integer or not. # Create an object x and assign a value 4.5 # Create two numeric objects y and z # Check whether y is greater than z or not y<-4 z<-7 result<-y>z result [1] FALSE With this kind of statement, you are asking R to evaluate the logical question “Is it true that y is greater than z?” The object storing the answer of above question is of type logical You can check the class of the object using class() Logical type objects can only take 2 values i.e. TRUE or FALSE. 30
  • 31. Vector a <- c(1,2,5.3,6,-2,4) a [1] 1.0 2.0 5.3 6.0 -2.0 4.0 Create three vectors: numeric, character and logical. All the elements of a vector must be of the same type. To convert any object into a vector type, use as.vector() function. # Numeric vector b <- c("one","two","three") b [1] "one" "two" "three" # Character vector c<-c(4,24,6,4, 2,7) l<-c>5 l [1] FALSE TRUE TRUE FALSE FALSE TRUE # Logical vector 31
  • 32. x<-matrix(c(2, 3, 4, 5, 6, 7),nrow=3,ncol=2,byrow=TRUE) x [,1] [,2] [1,] 2 3 [2,] 4 5 [3,] 6 7 x<-matrix(c(2, 3, 4, 5, 6, 7),nrow=3,ncol=2) x [,1] [,2] [1,] 2 5 [2,] 3 6 [3,] 4 7 Matrices byrow=TRUE fills the matrix row-wise matrix() function is used to create a matrix. nrow= and ncol= is used to specify the dimension of the matrix Note that the matrix is filled column-wise. To fill it row-wise see the next command Matrix is a two dimensional data structure. It is similar to vectors but additionally contains the dimension attribute. To convert any object into a matrix type, use as.matrix() function. # Create a matrix with 3 rows and 2 columns. 32
  • 33. Arrays a<-array(1:24,dim=c(3,4,2)) a , , 1 [,1] [,2] [,3] [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 , , 2 [,1] [,2] [,3] [,4] [1,] 13 16 19 22 [2,] 14 17 20 23 [3,] 15 18 21 24 array() is used to create array in R, where you give data as the first argument and a vector with the sizes of the dimensions as the second argument. dim= takes dimensions of the array. Note: Although the rows are given as the first dimension, the tables are filled column- wise. So, for arrays, R fills the columns, then the rows, and then the rest Arrays hold multidimensional rectangular data i.e. each row is the same length, and likewise for each column and other dimensions # Create an array with four columns, three rows and two “tables”. 33
  • 34. Data Frames x<-c(12,23,45) y<-c(13,21,6) z<-c("a","b","c") data<-data.frame(x,y,z) data x y z 1 12 13 a 2 23 21 b 3 45 6 c creates vectors x, y, z data.frame() function combines them in a table. object data is a dataframe containing three vectors x, y, z Data frame is a two-dimensional array like structure. It is a list of vectors of equal length. Data frames are the primary data structure in R. To convert any object into a data frame type, use as.data.frame() function. # Create a data frame from different vectors Data.frames are distinct from matrices because they can include heterogeneous data types among columns/variables. * 34
  • 35. Lists n=c(2, 3, 5) s=c("aa", "bb", "cc", "dd", "ee") x=list(n, s, 3) [[1]] [1] 2 3 5 [[2]] [1] "aa" "bb" "cc" "dd" "ee" [[3]] [1] 3 list() is used to create lists. List is a data structure having components of mixed data types. To convert any object into a list type, use as.list() function. # Create a list of 2 vectors and a numeric value 35 We retrieve a list slice by enclosing the index of the vector in a square bracket "[]" operator, for eg: x[2]
  • 37. Concatenating Strings - paste() paste() converts its arguments to character strings and concatenates (pastes) them to form one or several character strings. a<-"Hello" b<-'How' c<-"are you? " paste(a,b,c,sep="-") [1] "Hello-How-are you? " paste("y",1:4,sep=".") [1] "y.1" "y.2" "y.3" "y.4" sep= takes a character string that is used as a seperator. The default separator is a blank space (sep=" ") single character “y” is pasted with the sequence 1:4 and separator sep="." 37
  • 38. Extracting and Replacing - substring() substring("WELCOME TO THE WORLD OF R",12,14) [1] "THE" substring() extracts or replaces substrings in a character vector. # Extract ‘THE’ Characters from 12th to 14th position are extracted 38 Location<-c("Mumbai","Delhi","Mumbai","Kolkata","Delhi") substring(Location,1,3) [1] "Mum" "Del" "Mum" "Kol" "Del" # Extract first 3 letters string=c("G1:E001","G2:E002","G3:E003") substring(string,3)<-" " [1] "G1 E001" "G2 E002" "G3 E003" # Replacing ‘:’ with ‘ ’
  • 39. Replacing Substrings – sub() & gsub() string<-"She is a data scientist. She works with an MNC" sub("She","Sharon",string) [1] "Sharon is a data scientist. She works with an MNC" • sub() replaces the first instance of a substring sub() finds the first instance of the ‘She’ within string and replaces it with ‘Sharon’ 39 • gsub() replaces all instances of a substring. The g in gsub() stands for global. gsub("She","Sharon",string) [1] "Sharon is a data scientist. Sharon works with an MNC" gsub() does the same thing as sub(), but it replaces all instances of the substring(a global replace), not just the first
  • 41. Conditional Statements • Conditional Statements are useful in decision making when we want to execute a code only if a certain condition is satisfied • Condition is referred as a logical expression, when evaluated returns either True or False and depending on that the statements get executed • The simplest form is if statement. else and else if statements can also be used depending on the number of conditions to be checked 41
  • 42. if Statement If the condition is TRUE, the statement gets executed. But if it's FALSE, nothing happens. x<-5 if(x > 0){ print("Positive number") } [1] "Positive number" x<-"Hello there" if(is.character(x)) { print(x) } [1] "Hello there" # Check if x is a positive number or not # Print the value of the object if it is of a character type Comparison operator ‘>’ is used in condition. Here, x >0 returns TRUE; hence the print statement is executed is.character() returns TRUE or FALSE depending on whether the argument is of character type or not. Here x is a character, hence the condition returns TRUE and “Hello there” is printed 42
  • 43. x<- -5 if(x > 0){ print("Positive number") } else { print("Negative number") } [1] "Negative number" if..else Statement This condition evaluates to false; hence it will skip the body of if and the body of else will be executed. if..else Statements are used when you want to execute one block of code when a condition is true, and another block of code when it is false. There is an easier way to use if..else statement specifically for vectors. You can use ifelse() function instead. * # Check if x is a positive number or not and print the status 43
  • 44. ifelse() ifelse() Function is a vector equivalent form of the if..else statement a<-c(6,1,5,14) ifelse(a%%2==0,"even","odd") [1] "even" "odd" "odd" "even" ifelse() function can take a vector as an input and results into a vector. If the condition is TRUE it returns x else y Here, a is a vector, put in a condition to check and print even or odd depending on result. %% operator gives the remainder of the first vector with the second 44
  • 46. for Loop Syntax: for (Loop_Variable in Sequence) { Statement 1 Statement 2 .... } Loop_Variable sets a new value for each iteration of the loop. Sequence is a vector assigned to a Loop_Variable Statement 1, Statement 2,… Body of for consisting of block of program statements contained within curly braces 46 Loops are used when a block of codes needs to be executed multiple times (called iteration)
  • 47. for Loop • In for loop, the number of times the loop should be executed is typically defined in the initialisation part • Loop variable is created when the for statement runs • Loop variable is assigned the next element in the vector with each iteration and then the statements of the body are executed for ( i in 1:4) { print(i) } [1] 1 [1] 2 [1] 3 [1] 4 # Print numbers 1 to 4 47