R Programming - 3
R Programming - 3
Deleting Variables
Variables can be deleted by using the rm() function. Below we delete the variable
var.3. On printing the value of the variable error is thrown.
rm(var.3)
print(var.3)
rm(list = ls())
print(ls())
R- Operators
Types of Operators
We have the following types of operators in R programming −
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators
Arithmetic Operator:
Relational Operators
Following table shows the relational operators supported by R language. Each
element of the first vector is compared with the corresponding element of the second
vector. The result of comparison is a Boolean value.
v = c(2,5.5,6,9)
Checks if each element of the first vector is t = c(8,2.5,14,9)
greater than the corresponding element of print(v>t)
the second vector. print(v>=t)
< v = c(2,5.5,6,9)
t = c(8,2.5,14,9)
print(v < t)
Checks if each element of the first vector is PRINT(v<= t)
less than the corresponding element of the
second vector. it produces the following result −
[1] TRUE FALSE TRUE FALSE
[2] TRUE FALSE TRUE TRUE
==
v = c(2,5.5,6,9)
Checks if each element of the first vector is t = c(8,2.5,14,9)
equal to the corresponding element of the print(v == t)
second vector.
it produces the following result −
[1] FALSE FALSE FALSE TRUE
<=
v <- c(2,5.5,6,9)
Checks if each element of the first vector is t <- c(8,2.5,14,9)
less than or equal to the corresponding print(v<=t)
element of the second vector.
it produces the following result −
[1] TRUE FALSE TRUE TRUE
>=
v <- c(2,5.5,6,9)
Checks if each element of the first vector is t <- c(8,2.5,14,9)
greater than or equal to the corresponding print(v>=t)
element of the second vector.
it produces the following result −
[1] FALSE TRUE FALSE TRUE
!= v <- c(2,5.5,6,9)
t <- c(8,2.5,14,9)
Checks if each element of the first vector is print(v!=t)
unequal to the corresponding element of
the second vector. it produces the following result −
[1] TRUE TRUE TRUE FALSE
Logical Operators
Following table shows the logical operators supported by R language. It is applicable
only to vectors of type logical, numeric or complex. All numbers greater than 1 are
considered as logical value TRUE.
Each element of the first vector is compared with the corresponding element of the
second vector. The result of comparison is a Boolean value.
| v = c(3,0,TRUE,2+2i)
It is called Element-wise Logical OR t = c(4,0,FALSE,2+3i)
operator. It combines each element of the print(v|t)
first vector with the corresponding element it produces the following result −
of the second vector and gives a output
TRUE if one the elements is TRUE. [1] TRUE FALSE TRUE TRUE
! v = c(3,0,TRUE,2+2i)
It is called Logical NOT operator. Takes print(!v)
each element of the vector and gives the
opposite logical value. it produces the following result −
[1] FALSE TRUE FALSE FALSE
The logical operator && and || considers only the first element of the vectors and give
a vector of single element as output.
&& v = c(3,0,TRUE,2+2i)
t = c(1,3,TRUE,2+3i)
print(v&&t)
v = c(0,0,TRUE,2+2i)
Called Logical AND operator. Takes first
t = c(1,3,TRUE,2+3i)
element of both the vectors and gives the print(v&&t)
TRUE only if both are TRUE.
|| v = c(0,0,TRUE,2+2i)
t = c(0,3,TRUE,2+3i)
print(v||t)
v = c(7,0,TRUE,2+2i)
Called Logical OR operator. Takes first t = c(0,3,TRUE,2+3i)
element of both the vectors and gives the print(v||t)
TRUE if one of them is TRUE.
Assignment Operators
These operators are used to assign values to vectors.
Miscellaneous Operators
These operators are used to for specific purpose and not general mathematical or
logical computation.
: Colon v = 2:8
operator. It print(v)
creates the
series of it produces the following result −
numbers in [1] 2 3 4 5 6 7 8
sequence
for a vector.
%in% v1 <- 8
This v2 <- 12
operator is t <- 1:10
used to print(v1 %in% t)
identify if an print(v2 %in% t)
element
it produces the following result −
belongs to a
vector. [1] TRUE
[1] FALSE
%*%
This M = matrix( c(2,6,5,1,10,4), nrow = 2,ncol =
3,byrow = TRUE)
operator is
t = M %*% t(M)
used to print(t)
multiply a
matrix with it produces the following result −
its
[,1] [,2]
transpose.
[1,] 65 82
[2,] 82 117