<<

Operators – Arithmetic, Relational, Logical, Assignment

R Tutorial – We shall learn about R Operators – Arithmetic, Relational, Logical, Assignment and some of the Miscellaneous Operators that R provides.

R Operators

There are four main categories of Operators in R programming language. They are shown in the following picture :

We shall learn about these operators in detail with Example R programs.

R Arithmetic Operators

Arithmetic Operators are used to accomplish arithmetic operations. They can be operated on the data types Numericals, Integers, Complex Numbers. Vectors with these basic data types can also participate in arithmetic operations, during which the operation is performed on one to one element basis. Operator Description Usage

+ Addition of two a +

– Subtraction of second from first a – b

* Multiplication of two operands a * b

/ Division of first operand with second a / b

%% Remainder from division of first operand with second a %% b

%/% Quotient from division of first operand with second a %/% b

^ First operand raised to the power of second operand a^b

An example for each of the arithmetic operator on Numerical values is provided below :

r_op_arithmetic.R R Script File

# R Arithmetic Operators Example for integers

a#

par i

Output

$ Rscript r_op_arithmetic.R [1] 9.5

[1] 5.5 [$1 ]R s1c5ript r_op_arithmetic.R [1] 3.75 [[11]] 19..55 [[11]] 35.5 [1] 56.25 [1] 15 [1] 3.75 [1] 1.5 [1] 3 [1] 56.25

An example for each of the arithmetic operator on Vectors is provided below : r_op_arithmetic_vector.R R Script File

# R Operators - R Arithmetic Operators Example for vectors

a#

par i

Output

$ Rscript r_op_arithmetic_vector .R [ 1] 10 13 11 [$1 ]R s6c 5ri p1t r_op_arithmetic_vector.R [1] 16 36 30 [[11]] 41.00 01 32 .1215 1.20 [[11]] 06 15 11 [1] 4 2 1 [[11]] 1 64 3 65 3601 7776 [1] 4.00 2.25 1.20 [1] 0 1 1 [1] 4 2 1 [1] 64 6561 7776

R Relational Operators

Relational Operators are those that find out between the two operands provided to them. Following are the six relational operations R programming language supports.The output is boolean (TRUE or FALSE) for all of the Relational Operators in R programming language. Operator Description Usage

< Is first operand less than second operand a < b

> Is first operand greater than second operand a > b

== Is first operand equal to second operand a == b

<= Is first operand less than or equal to second operand a <= b

>= Is first operand greater than or equal to second operand a > = b

!= Is first operand not equal to second operand a!=b

An example for each of the relational operator on Numberical values is provided below :

r_op_relational.R R Script File

# R Operators - R Relational Operators Example for Numbers

a#

par ib ) # greater than

print ( a==b ) # equal tpor int ( aqbu a)l #to greater than pprriinntt (( aa>===bb )) ## equal to greater than or equal tporint ( a<=b ) # less than or equal to pprriinntt (( aa!>==bb ) )# # n gorte ater than or equal to equal to print ( a!=b ) # not equal to

Output

$ Rscript r_op_relational.R [1] FALSE

[1] TRUE [$1 ]R sFcArLipStE r_op_relational.R [1] FALSE [[11]] TFRAULESE [[11]] TTRRUUEE [1] FALSE [1] FALSE [1] TRUE [1] TRUE

An example for each of the relational operator on Vectors is provided below :

r_op_relational_vector.R R Script File

# R Operators - R Relational Operators Example for Numbers

# R Operators - R Relational Operators Example for Numbers

a <- c(7.5, 3, 5) b <- c(2, 7, 0)

print ( ab ) # greater than print ( a==b ) # equal to print ( a<=b ) # less than or equal to print ( a>=b ) # greater than or equal to print ( a!=b ) # not equal to

Output

$ Rscript r_op_relational_vector. R

[1] FALSE TRUE FALSE [$1 ]R s TcrRipUtE rF_AoLpS_Ere laTtRioUnEal_vector.R [1] FALSE FALSE F[A1]L SFEALSE TRUE FALSE [[11]] FTARLUSEE F ATLRSUEE TFRAULSEE [1] TRUE FALSE TRUE [[11]] TFRAULES ET RFUAEL STER UFAELSE [1] FALSE TRUE FALSE [1] TRUE FALSE TRUE [1] TRUE TRUE TRUE

R Logical Operators

Logical Operators in R programming language work only for the basic data types logical, numeric and complex and vectors of these basic data types.

Operator Description Usage

& Element wise logical AND operation. a & b

| Element wise logical OR operation. a | b

! Element wise logical NOT operation. !a

&& Operand wise logical AND operation. a && b

|| Operand wise logical OR operation. a || b

An example for each of the logical operators on Numerical values is provided below :

r_op_logical.R R Script File # R Operators - R Logical Operators Example for basic

logical elements # R Operators - R Logical Operators Example for basic logical elements a <- 0 # logical FALSE b <- 2 # logical TRUE a <- 0 # logical FALSE print ( a & b ) # logical AbN

OR element wise pprriinntt (( !aa &) #b )lo #gi cloagl ical AND element wise NOT element wise pprrinintt (( aa &|& b b ) )# # l ogical OR element wise lporginicta (l A!aN ) # logical NOT element wise consolidated for all eplerinmte n( tas && b ) # logical AND consolidated for all elements pprriinntt (( aa |||| bb )) ## logical OR consolidated for all elements logical OR consolidated f or all elements

Output

$ Rscript r_op_logical.R [1] FALSE [1] TRUE

[1] TRUE [$1 ]R sFcArLipStE r_op_logical.R [1] TRUE [1] FALSE [1] TRUE [1] TRUE [1] FALSE [1] TRUE

An example for each of the logical operators on Vectors is provided below :

R Script File

# R Operators - R Logical Operators Example for boolean v ectors # R Operators - R Logical Operators Example for boolean vectors a <- c(TRUE, TRUE, F ALSE, FALSE) ba <<-- cc((TTRRUUEE,, FTARLUSEE,, FALSE, FALSE) TRUE, FALSE) b <- c(TRUE, FALSE, TRUE, FALSE) print ( a & b ) # logical

AND element wise pprriinntt (( aa |& b b ) ) # # l ologgicicaal l AND element wise OR element wise pprrinintt (( !aa |) b# )lo #g icloagl ical OR element wise NpOrinTt e(l e!ma e)n #t wloisgeical NOT element wise print ( a && b ) # lopgrinicta l( AaN &D& b ) # logical AND consolidated for all elements cporinnsto (li daa t|e|d b f o) r# a lllo gical OR consolidated for all elements elements p rint ( a || b ) # logical OR consolidated for all elements Output

$ Rscript r_op_logical_vector.R [1] TRUE FALSE FALSE FALSE

$ Rscript r_op_logical_vector.R [1] TRUE FALSE FALSE FALSE [1] TRUE TRUE TRUE FALSE [1] FALSE FALSE TRUE TRUE [1] TRUE [1] TRUE

R Assignment Operators

Assignment Operators are those that help in assigning a value to the variable.

Operator Description Usage

= Assigns right side value to left side operand a = 3

<- Assigns right side value to left side operand a <- 5

-> Assigns left side value to right side operand 4 -> a

<<- Assigns right side value to left side operand a <<- 3.4

->> Assigns left side value to right side operand c(1,2) ->> a

An example for each of the assignment operators is provided below : r_op_assignment.R R Script File

# R Operators - R Assignment Operators

a = 2 print ( a )

a <- TRUE print ( a )

454 -> a print ( a )

a <<- 2.9 print ( a )

c(6, 8, 9) -> a print ( a )

# R Operators - R Assignment Operators

a = 2 print ( a )

a <- TRUE print ( a )

454 -> a print ( a )

a <<- 2.9 print ( a )

c(6, 8, 9) -> a print ( a )

Output

$ Rscript r_op_assignment.R [1] 2

[1] TRUE [$1 ]R s4c5r4ipt r_op_assignment.R [1] 2.9 [[11]] 62 8 9 [1] TRUE [1] 454 [1] 2.9 [1] 6 8 9

R Miscellaneous Operators

These operators does not fall into any of the categories mentioned above, but are significantly important during R programming for manipulating data.

Operator Description Usage

: Creates series of numbers from left operand to right operand a:b

%in% Identifies if an element(a) belongs to a vector(b) a %in% b

%*% Performs multiplication of a vector with its transpose A %*% t(A)

An example for each of the Miscellaneous operators is provided below : r_op_misc.R R Script File

# R Operators - R Misc Operators

a = 23:31 p#r inRt O( pae r)ators - R Misc Operators a = c(25, 27, 76) ba == 2273:31 print ( b %in% a ) print ( a ) M = matrix(c(1,2,3,4),

2, 2, TRUE) par i=nt c (( 2M5 ,% 2*7%, 7 t6()M) ) b = 27 print ( b %in% a )

M = matrix(c(1,2,3,4), 2, 2, TRUE) print ( M %*% t(M) )

Output

$ Rscript r_op_misc.R [1] 23 24 25 26 27 28 29 30 31

[1] TRUE $ R s[,c1r]ip [t, 2r_]op_misc.R [1,] 5 11 [[21,]] 2 31 12 4 25 26 27 28 29 30 31 [1] TRUE [,1] [,2] [1,] 5 11 [2,] 11 25

Conclusion :

In this R Tutorial, we have learnt about R Operators – R Arithmetic Operators, R Relational Operators, R Logical Operators, R Assignment Operators, R Miscellaneous Operators with example R commands and R script files.

Home - Get Started

⊩ R Tutorial

⊩ R Script File

⊩ R Working Directory

⊩ R Data Types

⊩ R Variables

⊩ R Operators

⊩ R Vectors

⊩ R Matrix

Decision Making Decision Making

⊩ R Decision Making

⊩ R if

⊩ R if..else

⊩ R if..else if...else

⊩ R switch

Loops

⊩ R Loops

⊩ R repeat loop

⊩ R while loop

⊩ R for loop

⊩ R break

Strings

⊩ R Strings

⊩ Find length of String in R

⊩ Extract Substring from a String in R

⊩ Concatenate two or more Strings in R

Functions

⊩ R Functions

DataFrame

⊩ R Data Frame

⊩ Sort R Data Frame by Column

⊩ For each row in an R Data Frame

⊩ Import Excel Data into R Dataframe

⊩ Convert R Dataframe to Matrix

⊩ R Dataframe - Delete Rows

⊩ R Dataframe - Drop Columns

⊩ R Dataframe - Add Column

⊩ R Dataframe - Change Column Name

⊩ R Dataframe - Remove Duplicate Rows

⊩ R Dataframe - Replace NA with 0

⊩ Convert Matrix to R Dataframe

Handling Data from Files

⊩ R CSV Files - Read, Filter, Write ⊩ R Read Excel XLS XLSX files

Charts & Graphs

⊩ R Pie Charts

⊩ R Line Graphs

Statistical Analysis

⊩ R Mean of a Vector

⊩ R Median of a Vector