R Programming: Presentation by Mrs.S.Jansi Rani, AP (SR - GR) Department of Information Technology
R Programming: Presentation by Mrs.S.Jansi Rani, AP (SR - GR) Department of Information Technology
R PROGRAMMING
Presentation By
Mrs.S.Jansi Rani, AP(Sr.Gr)
Department of Information Technology
ACKNOWLEDGEMENT
Materials: R Programming Related Internet Resources
R Programming 01/18/2021 2
INSTALLING R AND R STUDIO
R is freely downloadable from http://cran.r-project.org/
R Programming 01/18/2021 3
HISTORY
R is an implementation of S programming language
R was created by Ross Ihaka and Robert Gentleman at the university of Auckland, New Zealand
The project was conceived in 1992, with an initial version released in 1995 and a stable beta version in 2000
Another stable released on October 31st 2014, by R Development Core Team under GNU General Public
License.
R Programming 01/18/2021 4
COMPARISON WITH OTHER
LANGUAGE
R Programming Python
• It has more functions and packages • It has less functions and packages
• R is mostly use for data analysis • Generic programming tasks such as design of software’s
R Programming 01/18/2021 5
FEATURES OF R
PROGRAMMING
It is a simple and effective programming language which has been well developed.
It is a well-designed, easy, and effective language which has the concepts of user-defined, looping, conditional, and various I/O facilities.
It has a consistent and incorporated set of tools which are used for data analysis.
For different types of calculation on arrays, lists and vectors, R contains a suite of operators.
R is an interpreted language.
R Programming 01/18/2021 6
APPLICATIONS
Who uses R in their business:
R Programming 01/18/2021 7
INSTALLATION OF R
R Programming 01/18/2021 8
INSTALLATION OF R STUDIO
R studio is a user friendly front-end for R
Free and open source
Download from: http://www.rstudio.com/
R Programming 01/18/2021 9
RSTUDIO IDE
All the experiments going to work with the help of RStudio IDE.
Console Window
R Programming 01/18/2021 10
BASIC EXPERIMENTS
“Hello World” Program:
Syntax:
> string1<-"hello world"
> string1
[1] "hello world"
> print(string1)
[1] "hello world"
R Programming 01/18/2021 11
COMMENTS
Syntax:
> # First Program
> a=3
> print(a)
[1] 3
The above is an example for Single-line comment
Syntax:
> if(FALSE){"this is trick for multi-line comment"}
> string<-"Welcome to SREC"
> string
[1] "Welcome to SREC"
R doesnot support multi- line comment. So, we have a above trick to implement
multi-line comment
R Programming 01/18/2021 12
VARIABLES IN R
Variable are nothing but reserved memory locations to store values. This means that when you create a variable
R Programming 01/18/2021 13
ASSIGNMENT OF VARIABLE
Three operators used to assign the values to the variable.
R Programming 01/18/2021 14
DATA OPERATORS IN R
1. Arithmetic Operator
2. Assignment Operators
3. Relational Operators
4. Logical Operators
5. Special Operators
R Programming 01/18/2021 15
ARITHMETIC OPERATORS
a=2
> b=3
> a+b
[1] 5
> a-b
[1] -1
> a*b
[1] 6
> a/b
[1] 0.6666667
> a^b
[1] 8
> 5%%2
[1] 1
> 2%%2
[1] 0
> 5%/%2
[1] 2
> 7%/%2
[1] 3
R Programming 01/18/2021 16
ASSIGNMENT OPERATORS
> x=2
> x<-3
> y<<-5
>Y
Error: object 'Y' not found
> 3->z
>z
[1] 3
R Programming 01/18/2021 17
RELATIONAL OPERATOR
2>3
[1] FALSE
> 2<3
[1] TRUE
> 2==3
[1] FALSE
> 2==2
[1] TRUE
> 2!=2
[1] FALSE
> 2>=3
[1] FALSE
> 4>=3
[1] TRUE
> 2<=3
[1] TRUE
> 4<=3
[1] FALSE
R Programming 01/18/2021 18
LOGICAL OPERATOR x<-TRUE
>x
[1] TRUE
> !x
[1] FALSE
> Y<- FALSE
> x&y
[1] TRUE
> x&Y
[1] FALSE
> x|Y
[1] TRUE
# Zero is considered FALSE and non-zero numbers are taken as TRUE
2&3
[1] TRUE
> 3&3
[1] TRUE
> x<-3
> y<-4
> z<-3
> x&z
[1] TRUE
R Programming 01/18/2021 19
SPECIAL OPERATORS
x<-2:5
>x
[1] 2 3 4 5
> x[1]
[1] 2
> x[0]
integer(0)
> y=3
> y%in%x
[1] TRUE
R Programming 01/18/2021 20
CHECK & ANSWER
X<- 2:8
Y<- 3:4
X+Y
X+Y->Z
Z
X<-1:4
X>2
log(-1)
R Programming 01/18/2021 21
DATATYPE
Note: We do not need to declare a variable before using them.
Data type
Data
Vectors Lists Matrices Array Factors
Frames
R Programming 01/18/2021 22
VECTORS
A vector is a sequence of data elements of the same basic type
Vectors 2,4,3
3+4i Complex Numeric
Integer
R Programming 2L,3L,4L 01/18/2021 23
VECTOR OPERATIONS
R Programming 01/18/2021 24
LISTS
List are the R objects which contain elements of different types like –
numbers, strings, vectors and another list inside it.
>> aa <-
<- c(1,2)
c(1,2)
>> bb <-
<- c('a','b')
c('a','b')
>> cc <-
<- list(a,b,TRUE)
list(a,b,TRUE)
>> cc
[[1]]
[[1]]
[1]
[1] 11 22
[[2]]
[[2]]
[1]
[1] "a"
"a" "b"
"b"
[[3]]
[[3]]
[1]
[1] TRUE
TRUE
R Programming 01/18/2021 25
> list1 <- list(1,2,3)
> list2 <- list('a','b',TRUE)
LISTS OPERATIONS
> mergelist <- c(list1,list2)
> mergelist
[[1]]
[1] 1
[[2]]
[1] 2
[[3]]
[1] 3
[[4]]
[1] "a"
[[5]]
[1] "b"
[[6]]
[1] TRUE
> list1[2]
[[1]]
[1] 2
> mergelist[2]
[[1]]
[1] 2
> list3 <- list(list1,list2)
> list3[2]
[[1]]
[[1]][[1]]
[1] "a"
[[1]][[2]]
[1] "b"
[[1]][[3]]
R Programming [1] TRUE 01/18/2021 26
DIFFERENCE
Vectors Lists
Vector stores elements of the same type or A list holds different data such as Numeric,
converts implicitly Character, Logical, etc
R Programming 01/18/2021 27
ARRAYS
>> vector1
vector1 <-
<- c(1,2,3)
c(1,2,3)
>> vector2
vector2 <- c(4,5,6,7,8,9)
<- c(4,5,6,7,8,9)
>> result
result <-
<- array(c(vector1,vector2),
array(c(vector1,vector2), dim=c(3,2,
dim=c(3,2,
2))
2))
>> result
result
,, ,, 11
Arrays are the R data objects which [,1]
[,1] [,2]
[,2]
[1,]
[1,] 11 44
[2,] 22 55
can store data in more than two [2,]
[3,]
[3,] 33 66
,, ,, 22
dimensions
[,1]
[,1] [,2]
[,2]
[1,]
[1,] 77 11
[2,] 88 22
It takes vectors as input and uses the [2,]
[3,]
[3,] 99 33
>> mat1
mat1 <-
<- array(c(1,2,3,4),dim=c(2,2))
values in the dim parameter to >> mat1
mat1
array(c(1,2,3,4),dim=c(2,2))
[,1]
[,1] [,2]
[,2]
[1,] 11 33
create an array [1,]
[2,]
[2,] 22 44
>> mat1
mat1 <-
<- array(c(1,2,3,4),dim=c(2,2,2))
array(c(1,2,3,4),dim=c(2,2,2))
>> mat1
mat1
,, ,, 11
E,g:- We create an array of
[,1]
[,1] [,2]
[,2]
[1,] 11 33
dimension (2,3,3), then it creates 3 [1,]
[2,]
[2,] 22 44
,, ,, 22
rectangular matrices each with 2
[,1]
[,1] [,2]
[,2]
[1,]
[1,] 11 33
rows and 3 columns [2,]
[2,] 22 44
R Programming 01/18/2021 28