R Unit 2 Notes
R Unit 2 Notes
R programming Unit 2
Operators in R: An operator is a symbol that performs some specific operation.
There are 5 different types of operators in r, namely, Arithmetic operators,
Assignment operators, Comparison Operators, Logical and Miscellaneous operators.
Arithmetic Operators: They are used to perform simple arithmetic operations as
described in table below:
GOGTE BCA 1
R programming Unit 2
Example: if a=10, b=5 then the relation expression a<b will return FALSE
GOGTE BCA 2
R programming Unit 2
Example: if a=10, b=20, c=5 and d=2 then the expression (a+b)>30 && (c+d)>5
will return TRUE
Miscellaneous Operators:Miscellaneous operators are used to manipulate data.
The various miscellaneous operators are listed below:
GOGTE BCA 3
R programming Unit 2
if statement: The if statement is used to test conditions. There are three forms of if
statement, namely, simple if, if…else and if…else if.
Simple if statement: In simple if the condition is tested first. If it is true then the
statement block following it will be executed.
Syntax:
Example:
if(ch==1){
print(“hello”)
}
if…else statement: In this type of if statement the statement block I is executed
when the condition is true and statement block II is executed when the condition is
false.
Syntax:
GOGTE BCA 4
R programming Unit 2
Example:
if(n>0){
print(“Number is positive”)
}else{
print(“Number is negative”)
}
if…else…if statement: This type of if statement is used to test multiple conditions.
Here, whenever a matching condition is found the corresponding statement block is
executed. However, when none of the conditions match the else body is executed.
Example:
if(n>0){
print(“Number is positive”)
}else if(n<0){
print(“Number is Negative”)
}else{
print(“Given number is a zero”)
}
GOGTE BCA 5
R programming Unit 2
GOGTE BCA 6
R programming Unit 2
Example:
for (val in 1: 5)
{
print(val)
}
While loop: while loop is an entry controlled loop. Here the condition is tested at
the beginning of the loop. If the condition is true then the vbbody of the loop is
executed otherwise the loop is terminated. Hence, while loop used when the exact
number of iterations of a loop is not known before hand.
GOGTE BCA 7
R programming Unit 2
Syntax:
Example:
n=1
while (n <= 5)
{
print(n)
n= n + 1
}
Repeat loop: Repeat loop in R is used to iterate over a block of code multiple
number of times. The keyword used for the repeat loop is 'repeat'. It executes a block
of statement repeated until a break statement is found. Since there is no condition
tested at the beginning or end of the loop, it is necessary to include a conditional
statement within the loop, otherwise it may result in an infinite loop
Syntax:
Example:
n=1
repeat
{
print(n)
n=n+1
GOGTE BCA 8
R programming Unit 2
if(n > 5)
{
break
}
}
Example:
for (val in 1: 5)
{
if (val == 3)
{
# using break keyword
break
}
print(val)
}
Output: 1 2
Next:It is also a keyword and is used to skip a particular iteration in the loop.
Example:
for (val in 1: 5)
{
if (val == 3)
{
next
}
print(val)
}
Output: 1 2 4 5
Example:
for (val in 1: 5)
{
if (val == 3)
{
stop()
}
print(val)
}
User-Defined functions- They are defined and created by the user/ Programmer.
Creating Function: A user defined function can be created using the following
Syntax:
funcname<-function(argument list)){
statements
statements
Statements
return(value)
}
Where,
Funcname- is an identifier
GOGTE BCA 10
R programming Unit 2
Argumenet list- It is optional and indictae the data values being passed to the
function
Return-It is used to return a value to the called function
Example:
greet <- function(name) {
cat("Hello World!",name)
}
Syntax:
funcname(arguments)
Example:
greet(“jhon”)
However, in R, the functiona call can contain argument names if parameter order is
not followed.
Example:
Rectangle<-function(length=5, breadth=4){
area<-length * breadth
return(area)
}
#argument names are used because order is changed
print(Rectangle(breadth = 8, length = 4))
GOGTE BCA 11
R programming Unit 2
Example:
Rectangle<-function(length=5, breadth=4){
area<-length * breadth
return(area)
}
Returning Complex Objects: A function can return complex numbers using the
return statement:
Example:
add<-function(a,b){
c<-a+b
return( c ) #c contains a complex bnumber
}
print(add(2+3i, 3+4i))
Output:
5+7i
Programming Examples:
#Program to check whether a given number is even or odd
cat("Enter a number")
n<-as.integer(readline())
if(n %% 2 == 0){
print("even number")
} else{
print("Odd number")
}
GOGTE BCA 12
R programming Unit 2
revnum<-function(n){
rev=0
while(n>0)
{
dig<- n%%10
rev=(rev*10)+dig
n<-n%/%10
}
return(rev)
}
cat("enter a number")
n<-as.numeric(readline())
r= revnum(n)
cat("Reversed number is ",r)
if(r==n){
cat("\n",n,"is a Palindrome number")
}else{
cat("\n",n,"is not a Pailndrome")
}
GOGTE BCA 13
R programming Unit 2
if(n < 0) {
print("Not possible for negative numbers")
} else if(n == 0) {
print("The factorial of 0 is 1")
} else {
for(i in 1:n) {
f=f*i
}
cat("The factorial of", n ,"is",f)
}
# Program Illustrate with for loop and stop on condition, to print the error
message.
sum <- 0
n <- as.numeric(readline("How many elements: "))
for(i in 1:n)
{
GOGTE BCA 14