Prof. Neeraj Bhargava
Pramod Singh Rathore
Department of Computer Science
School of Engineering & System Sciences,
MDS University Ajmer, Rajasthan, India
1
Control Structures in R
Control
Structures
 If…else loop
 For loop
 While loop
 Functions
2
If….else loop
3
If…else loop works on conditions
It will always s evaluate something and return a logical output, based on this a
set of codes will be executed.
For loop
4
When you want to iterate over a range of values or series of numbers names
we use fro loop.
Most of the time we use for loops only
We need to store each value mentioned in a range in a variable
for(i in 1:1000) {
print(i)
}
While loop
5
While loop and for loop are almost same
1. i=1
2. while(i<100){
3. print(i)
4. i=i+1
5. }
Functions
6
A function is a set of statemnts organized together to perform a specific task
R function is created by using a deywork
‘function’
Function_name=function(arg1,arg2…..){
Function body
}
If…else
7
data(“iris”)
View(iris) inbuilt dataset
str(iris)
#mean,median,standard deviation
Desc_setosa=matrix(nrow=5,ncol=4,
dimnames=list(c(“mean”,”median”,”mode”,”standard deviation”,”no
ofcases”),colnames(iris)[1:4]))
Lists
8
List is similar as vector
It is a data structure have components of mixed data types
Data frames
9
A data frame is used fro storing data tables
It is a special case of list which as each components of equal length
Each component form the column and contents of the components form the
rows
Data frame can be define by data.frame() function
Factors
10
Factor is a data structure used for fields that takes only predefined, finite
number of values (catergorialc data)
Factor variable will always have levels
While using data.frame() all the character data are converted into factor
How to handle vector in R
11
c(2,3,4) output : 2 3 4
Class(c(2,3,4)) numeric
C(2,3,”pramod”) “2” “3” “pramod”
class(C(2,3,”pramod”)) character
Length(c(2,3,4)) 3
a = c(2,3,4)
b= c(“a”,”b”,”c”)
vec=c(a,b) “2”,”3”,”4”,”a”,”b”,”c”
class(vec) character
Class(a) numeric
How to handle vector in R
12
A= c(1,2,3,4)
b= c(-1,-2,-3,-4)
Vect=a+b 0 0 0 0
v1=c(1,2,3)
v2=c(4,5,6,7)
v3=v1+v2 warning message
5 7 9 8 recycling rule
How to handle vector in R
13
v4= c(1:10)
v4[5] 5
V4[-5] 1 2 3 4 6 7 8 9 10
V4(3:5) 3 4 5
V4[c(3:5),10] 3 4 5 10 range indexing
Assignment 1
 What is data types in R?
 Explain each data type with suitable example
14
Queries ????
15
16

Control statements

  • 1.
    Prof. Neeraj Bhargava PramodSingh Rathore Department of Computer Science School of Engineering & System Sciences, MDS University Ajmer, Rajasthan, India 1 Control Structures in R
  • 2.
    Control Structures  If…else loop For loop  While loop  Functions 2
  • 3.
    If….else loop 3 If…else loopworks on conditions It will always s evaluate something and return a logical output, based on this a set of codes will be executed.
  • 4.
    For loop 4 When youwant to iterate over a range of values or series of numbers names we use fro loop. Most of the time we use for loops only We need to store each value mentioned in a range in a variable for(i in 1:1000) { print(i) }
  • 5.
    While loop 5 While loopand for loop are almost same 1. i=1 2. while(i<100){ 3. print(i) 4. i=i+1 5. }
  • 6.
    Functions 6 A function isa set of statemnts organized together to perform a specific task R function is created by using a deywork ‘function’ Function_name=function(arg1,arg2…..){ Function body }
  • 7.
    If…else 7 data(“iris”) View(iris) inbuilt dataset str(iris) #mean,median,standarddeviation Desc_setosa=matrix(nrow=5,ncol=4, dimnames=list(c(“mean”,”median”,”mode”,”standard deviation”,”no ofcases”),colnames(iris)[1:4]))
  • 8.
    Lists 8 List is similaras vector It is a data structure have components of mixed data types
  • 9.
    Data frames 9 A dataframe is used fro storing data tables It is a special case of list which as each components of equal length Each component form the column and contents of the components form the rows Data frame can be define by data.frame() function
  • 10.
    Factors 10 Factor is adata structure used for fields that takes only predefined, finite number of values (catergorialc data) Factor variable will always have levels While using data.frame() all the character data are converted into factor
  • 11.
    How to handlevector in R 11 c(2,3,4) output : 2 3 4 Class(c(2,3,4)) numeric C(2,3,”pramod”) “2” “3” “pramod” class(C(2,3,”pramod”)) character Length(c(2,3,4)) 3 a = c(2,3,4) b= c(“a”,”b”,”c”) vec=c(a,b) “2”,”3”,”4”,”a”,”b”,”c” class(vec) character Class(a) numeric
  • 12.
    How to handlevector in R 12 A= c(1,2,3,4) b= c(-1,-2,-3,-4) Vect=a+b 0 0 0 0 v1=c(1,2,3) v2=c(4,5,6,7) v3=v1+v2 warning message 5 7 9 8 recycling rule
  • 13.
    How to handlevector in R 13 v4= c(1:10) v4[5] 5 V4[-5] 1 2 3 4 6 7 8 9 10 V4(3:5) 3 4 5 V4[c(3:5),10] 3 4 5 10 range indexing
  • 14.
    Assignment 1  Whatis data types in R?  Explain each data type with suitable example 14
  • 15.
  • 16.