R Operators - 03
R Operators - 03
R Programming Operators
Class 07 Presented by
Dr. Selvi C
Assistant
Professor
IIIT Kottayam
Operators in R
• In computer programming, an operator is a symbol which represents an action. An
operator is a symbol which tells the compiler to perform
specific logical or mathematical manipulations. R programming is very rich in built-
in operators.
• In R programming, there are different types of operator, and each operator performs
a different task. For data manipulation, There are some advance operators also such
as model formula and list indexing.
• There are the following types of operators used in R:
• Arithmetic Operators
• Relational Operators
• Logical Operators
• Assignment Operators
• Miscellaneous Operators
2
Arithmetic Operators
Arithmetic operators are the symbols which are used to represent arithmetic math operations. The operators act on each and
every element of the vector. There are various arithmetic operators which are supported by R.
1. + This operator is used to add two vectors in R. a <- b <- c(11, 5, 3) print(a+b) It will give us the following output: [1]
c(2, 3.3, 4) 13.0 8.3 5.0
2. - This operator is used to divide a vector from another b <- c(11, 5, 3) print(a-b) It will give us the following output: [1] -
one. a <- c(2, 3.3, 4) 9.0 -1.7 3.0
3. * This operator is used to multiply two vectors with b <- c(11, 5, 3) print(a*b) It will give us the following output: [1]
each other. a <- c(2, 3.3, 4) 22.0 16.5 4.0
4. / This operator divides the vector from another one. a b <- c(11, 5, 3) print(a/b)It will give us the following output: [1]
<- c(2, 3.3, 4) 0.1818182 0.6600000 4.0000000
5. %% This operator is used to find the remainder of the b <- c(11, 5, 3) print(a%%b) It will give us the following output:
first vector with the second vector. a <- c(2, 3.3, 4) [1] 2.0 3.3 0
6. %/% This operator is used to find the division of the first a <- c(2, 3.3, 4) b <- c(11, 5, 3) print(a%/%b) It will give us the
vector with the second(quotient). following output: [1] 0 0 4
7. ^ This operator raised the first vector to the exponent b <- c(11, 5, 3) print(a^b) It will give us the following output: [1]
of the second vector. a <- c(2, 3.3, 4) 0248.0000 391.3539 4.0000
3
Relational Operators
• A relational operator is a symbol which defines some kind of relation between two entities. These include numerical equalities and inequalities. A
relational operator compares each element of the first vector with the corresponding element of the second vector. The result of the comparison will
be a Boolean value. There are the following relational operators which are supported by R:
1. > This operator will return TRUE when every element in the first vector is a <- c(1, 3, 5) b <- c(2, 4, 6) print(a>b) It will
greater than the corresponding element of the second vector. give us the following output: [1] FALSE FALSE
FALSE
2. < This operator will return TRUE when every element in the first vector is a <- c(1, 9, 5) b <- c(2, 4, 6) print(a<b) It will
less then the corresponding element of the second vector. give us the following output: [1] FALSE TRUE
FALSE
3. <= This operator will return TRUE when every element in the first vector is a <- c(1, 3, 5) b <- c(2, 3, 6) print(a<=b) It
less than or equal to the corresponding element of another vector. will give us the following output: [1] TRUE
TRUE TRUE
4. >= This operator will return TRUE when every element in the first vector is a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It
greater than or equal to the corresponding element of another vector. will give us the following output: [1] FALSE
TRUE FALSE
5. == This operator will return TRUE when every element in the first vector is a <- c(1, 3, 5) b <- c(2, 3, 6) print(a==b) It
equal to the corresponding element of the second vector. will give us the following output:[1] FALSE
TRUE FALSE
6. != This operator will return TRUE when every element in the first vector is a <- c(1, 3, 5) b <- c(2, 3, 6) print(a>=b) It
not equal to the corresponding element of the second vector. will give us the following output: [1] TRUE
FALSE TRUE
4
Logical Operators
• The logical operators allow a program to make a decision on the basis of multiple conditions. In the program, each operand is
considered as a condition which can be evaluated to a false or true value. The value of the conditions is used to determine the
overall value of the op1 operator op2. Logical operators are applicable to those vectors whose type is logical, numeric, or
complex.
• The logical operator compares each element of the first vector with the corresponding element of the second vector.
S. No Operator Description Example
1. & This operator is known as the Logical AND operator. This operator takes a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE,
the first element of both the vector and returns TRUE if both the 2+3i) print(a&b) It will give us the
elements are TRUE. following output: [1] TRUE FALSE TRUE
TRUE
2. | This operator is called the Logical OR operator. This operator takes the a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE,
first element of both the vector and returns TRUE if one of them is TRUE. 2+3i) print(a|b) It will give us the following
output: [1] TRUE TRUE TRUE TRUE
3. ! This operator is known as Logical NOT operator. This operator takes the a <- c(3, 0, TRUE, 2+2i) print(!a) It will
first element of the vector and gives the opposite logical value as a result. give us the following output: [1] FALSE
TRUE FALSE FALSE
4. && This operator takes the first element of both the vector and gives TRUE a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE,
as a result, only if both are TRUE. 2+3i) print(a&&b) It will give us the
following output: [1] TRUE
5. || This operator takes the first element of both the vector and gives the a <- c(3, 0, TRUE, 2+2i) b <- c(2, 4, TRUE,
result TRUE, if one of them is true. 2+3i) print(a||b) It will give us the
following output: [1] TRUE
5
Assignment Operators
An assignment operator is used to assign a new value to a variable. In R, these operators are
used to assign values to vectors. There are the following types of assignment
1. <- or = or <<- These operators are known as left a <- c(3, 0, TRUE, 2+2i) b <<- c(2, 4, TRUE, 2+3i) d = c(1, 2,
assignment operators. TRUE, 2+3i) print(a) print(b) print(d) It will give us the following
output: [1] 3+0i 0+0i 1+0i 2+2i [1] 2+0i 4+0i 1+0i 2+3i [1] 1+0i
2+0i 1+0i 2+3i
2. -> or ->> These operators are known as right c(3, 0, TRUE, 2+2i) -> a c(2, 4, TRUE, 2+3i) ->> b print(a) print(b)
assignment operators. It will give us the following output: [1] 3+0i 0+0i 1+0i 2+2i [1]
2+0i 4+0i 1+0i 2+3i
6
Miscellaneous Operators
• Miscellaneous operators are used for a special and specific purpose. These operators are not used for general mathematical or logical computation.
There are the following miscellaneous operators which are supported in R
1. : The colon operator is used to create the series of v <- 1:8 print(v) It will give us the following output: [1] 1 2 3 4
numbers in sequence for a vector. 5678
2. %in% This is used when we want to identify if an element a1 <- 8 a2 <- 12 d <- 1:10 print(a1%in%t) print(a2%in%t) It
belongs to a vector. will give us the following output: [1] FALSE [1] FALSE
3. %*% It is used to multiply a matrix with its transpose. (all M=matrix(c(1,2,3,4,5,6), nrow=2, ncol=3, byrow=TRUE) T=m
matrix operation) %*%T(m) print(T) It will give us the following output: 14 32 32
77
7
ICS422 Applied Predictive Analytics [3-
0-0-3]
R Programming Math
Presented by
Dr. Selvi C
Assistant
Professor
IIIT Kottayam
Built-in Math Functions
• R also has many built-in math functions that allows you to
perform mathematical tasks on numbers.
• For example, the min() and max() functions can be used to find
the lowest or highest number in a set:
Example
• max(5, 10, 15)
10
ICS422 Applied Predictive Analytics [3-
0-0-3]
R Programming String
Presented by
Dr. Selvi C
Assistant
Professor
IIIT Kottayam
String Literals
• Strings are used for storing text.
• A string is surrounded by either single quotation marks, or double quotation marks:
• "hello" is the same as 'hello':
Example
• "hello"
'hello'
12
Assign a String to a Variable
• Assigning a string to a variable is done with the
variable followed by the <- operator and the string:
Example
• str <- "Hello"
str # print the value of str
13
Multiline Strings
• You can assign a multiline string to a variable like this:
Example
• str <- "a
b
c"
• str # print the value of str
• [1] "a\nb\nc"
14
• However, note that R will add a "\n" at the end of each line break. This is
called an escape character, and the n character indicates a new line.
• If you want the line breaks to be inserted at the same position as in the
code, use the cat() function:
Example
• str <- "a
b
c"
cat(str)
a
b
c 15
String Length
• There are many usesful string functions in R.
• For example, to find the number of characters in a string, use
the nchar() function:
Example
• str <- "Hello World!"
nchar(str)
• [1] 12
16
Check a String
• Use the grepl() function to check if a character or a sequence of
characters are present in a string:
Example
• str <- "Hello World!"
grepl("H", str)
grepl("Hello", str)
grepl("X", str)
• [1] TRUE
[1] TRUE
[1] FALSE
17
Combine Two Strings
• Use the paste() function to merge/concatenate two strings:
Example
• str1 <- "Hello"
str2 <- "World"
paste(str1, str2)
• [1] "Hello World"
18
Escape Characters
• To insert characters that are illegal in a string, you must use an
escape character.
• An escape character is a backslash \ followed by the character you
want to insert.
• An example of an illegal character is a double quote inside a string
that is surrounded by double quotes:
Example
• str <- "We are the so-called "Vikings", from the north."
str
Result:
• Error: unexpected symbol in "str <- "We are the so-called "Vikings"
19
To fix this problem, use the escape character \":
Example
The escape character allows you to use double quotes when you normally
would not be allowed:
str <- "We are the so-called \"Vikings\", from the north."
Str
cat(str)
21
ICS422 Applied Predictive Analytics [3-
0-0-3]
23
Contd...
• You can also compare two variables:
Example
• a <- 10
b <- 9
a>b
Example
a <- 200
b <- 33
if (b > a) {
print ("b is greater than a")
} else {
print("b is not greater than a")
}
24
Any
Queries?
Thank you