Operators in R
Parag Verma
11th Dec, 2019
Concept of Vectorization
To speed up operation in R, most computations are done in parallel.Meaning, that instead of looping through each item one by one, all the elements are engaged simultaneously. This helps in expediting the process and write code that is efficient, crisp and easier to read.Below we will discuss operators which involve operations that are Vectorized in nature
Operators in R
Starting point of any analyses is R is to store the data in various data types such as Vectors,lists, data frame etc. Once the data is set up, we might want to do certain meaningful operations on it such as mathematical, logical and so on.In this document we will look at how we can perform various operations in R
Arithematic Operators
These are used to perform all sorts of mathematical operations such as addition, substraction, multiplocation and so on.
x<-c(2,4,6)
y<-c(1,2,3)
# Addition
x+y
[1] 3 6 9
# Substraction
x-y
[1] 1 2 3
# Multiplication
x*y
[1] 2 8 18
# Division
x/y
[1] 2 2 2
# Modulo (give the remainder when x is divided by y)
x %% y
[1] 0 0 0
Relational Operator
It is used to do an element vise comparison between two vectors.Output is always a TRUE or a FALSE
x<-c(2,4,6)
y<-c(1,2,3)
# > operator
x>y
[1] TRUE TRUE TRUE
# < operator
x<y
[1] FALSE FALSE FALSE
# Equal to Operator
x==y
[1] FALSE FALSE FALSE
# Less than or equal to
x <= y
[1] FALSE FALSE FALSE
# Greater than or equal to
x >= y
[1] TRUE TRUE TRUE
# Unequal to
x != y
[1] TRUE TRUE TRUE
Logical Operators
It applies only to vectors of the type Numeric, Logical or Complex All vectors 1 or greater than 1 are considered logical T.It does an element vise logical operation.Output is always a TRUE or a FALSE
x<-c(0,2,3)
y<-c(1,0,9)
# Logical And Operation
x & y
[1] FALSE FALSE TRUE
# Logical And Operation
x | y
[1] TRUE TRUE TRUE
# Logical Not operation
! x
[1] TRUE FALSE FALSE
# && operator
# It considers only the first element of the operands and outputs the results
x && y
[1] FALSE
# || operator
# It considers only the first element of the operands and outputs the results
x || y
[1] TRUE
Cyclicity Concept
It applies to cases where the vectors are of different lengths and we want to apply operators on these vectors
x<-c(2,4,6,8)
y<-c(1,2,3)
# Addition
x+y
[1] 3 6 9 9
# 'Since there is no element a the 4th position in y'
# 'therefore it takes the element from the first position of y ie 1 and'
# 'adds it to 8. Similar explanation can be used for the subsequent operations'
# Substraction
x-y
[1] 1 2 3 7
# Multiplication
x*y
[1] 2 8 18 8
# Division
x/y
[1] 2 2 2 8
# Modulo (give the remainder when x is divided by y)
x %% y
[1] 0 0 0 0
# > operator
x>y
[1] TRUE TRUE TRUE TRUE
# < operator
x<y
[1] FALSE FALSE FALSE FALSE
# Equal to Operator
x==y
[1] FALSE FALSE FALSE FALSE
# Less than or equal to
x <= y
[1] FALSE FALSE FALSE FALSE
# Greater than or equal to
x >= y
[1] TRUE TRUE TRUE TRUE
# Unequal to
x != y
[1] TRUE TRUE TRUE TRUE
Link to Previous R Blogs
Blog 1-Vectors,Matrics, Lists and Data Frame in R https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
No comments:
Post a Comment