R Operators and R Loops
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
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)
for(i in 1:10) {
print(i)
for(i in 1:10) {
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))
}
}