R Operators and R Loops
By
Mrs.Subhasheni A
Assistant Professor
Sri Ramakrishna College Of Arts & Science
R OPERATORS
Operators in R are symbols or keywords that
perform operations on data. They are used to
manipulate variables and values in various ways,
such as mathematical calculations, logical
operations, and data transformations.
TYPES OF OPERATORS
Arithmetic Operators
+ (Addition): Adds two numbers.
- (Subtraction): Subtracts the second number from the first.
* (Multiplication): Multiplies two numbers.
/ (Division): Divides the first number by the second.
^ or ** (Exponentiation): Raises the number to the power of the
second.
%% (Modulus): Returns the remainder of division.
%/% (Integer Division): Performs integer division, returns the
quotient.
CONTN..
Relational Operators
== (Equality): Checks if two values are equal.
!= (Not Equal): Checks if two values are not equal.
> (Greater Than): Checks if the first value is greater than the
second.
< (Less Than): Checks if the first value is less than the second.
>= (Greater Than or Equal To): Checks if the first value is greater
than or equal to the second.
<= (Less Than or Equal To): Checks if the first value is less than
or equal to the second.
CONTN..
Logical Operators
& (AND): Returns TRUE if both conditions are TRUE.
| (OR): Returns TRUE if at least one condition is TRUE.
! (NOT): Reverses the logical state (TRUE becomes FALSE and vice
versa).
Assignment Operators
<- or = (Assignment): Assigns a value to a variable.
->: Assigns a value to a variable on the right-hand side.
CONTN..
Miscellaneous Operators
$ (Subset): Used to access specific elements of a list or data
frame by name.
[] (Indexing): Extracts elements from vectors, lists, or
matrices based on index.
[[]] (List Indexing): Extracts elements from a list and returns
them in the original format.
R LOOPS
In R, loops allow you to repeatedly execute a block
of code multiple times. Loops are essential for
performing repetitive tasks without writing repetitive
code.
TYPES OF LOOPS
For Loop
The for loop is used to iterate over a sequence (such as a vector,
list, or range of numbers).
SYNTAX
For(i in 1:5)
print(i)
This loop will print the numbers 1 to 5.
Commonly used for iterating over vectors, data frames, or matrices.
WHILE LOOP
The while loop executes a block of code as
long as the condition is TRUE.
SYNTAX
i <- 1
while(i <= 5) {
print(i)
i <- i + 1
}
The loop will print numbers 1 to 5 and
continues as long as the condition holds.
REPEAT LOOP
The repeat loop is used for infinite iteration
unless a break condition is met.
SYNTAX
i <- 1
repeat {
print(i)
i <- i + 1
if(i > 5) break
}
This loop will keep executing until the
condition to break out of the loop (i.e., i > 5)
is met.
BREAK AND NEXT
break: Exits the loop completely when a condition is met.
next: Skips the current iteration and moves to the next one in the loop.
SYNTAX
for(i in 1:10) {
if(i == 5) break # Exits loop when i is 5
print(i)
for(i in 1:10) {
if(i == 5) next # Skips iteration when i is 5
print(i)
}
NESTED LOOPS
You can also use loops inside other loops (nested loops).
SYNTAX
for(i in 1:3) {
for(j in 1:2) {
print(paste("i =", i, "j =", j))
}
}
The outer loop iterates over values of i, and for each
iteration, the inner loop iterates over values of j.