R Operators

Operators are used to calculate some basic operations in R.

Arithmetic Operators:

+ -- Addition - -- Subtraction * -- Multiplication / -- Division ** -- Exponentiation (Power of vector) ^ -- Exponentiation (Power of vector) %% -- Modulus %/% -- Integer Divide %*% -- Matrix Product <- -- Left Assignment Operator (Assignment Operator) -> -- Right Assignment Operator (Assignment Operator)

Examples:

> a<-c(1,2,3) > b<-c(4,5,6) > a+b [1] 5 7 9 > a-b [1] -3 -3 -3 > a*b [1] 4 10 18 > a/b [1] 0.25 0.40 0.50 > a**2 [1] 1 4 9 > a^2 [1] 1 4 9 > a%%b [1] 1 2 3 > a%*%b [,1] [1,] 32 > a%/%b [1] 0 0 0

Relational Operators

< --> Less than
> –> Greater than
== –> Equal to
<= --> Less than or equal to
>= –> Greater than or equal to
!= –> Not equal to

Examples:

> a<-c(1,2,3) > b<-c(4,5,6) > a a>b [1] FALSE FALSE FALSE > a==b [1] FALSE FALSE FALSE > a<=b [1] TRUE TRUE TRUE > a>=b [1] FALSE FALSE FALSE > a!=b [1] TRUE TRUE TRUE

Logical Operators

& –> Elementwise logical AND
&& –> Vector logical AND
| –> Elementwise Logical OR
|| –> Logical OR
! –> Logical Negation

> a<-c(1,2,3) #Define Vector a > b<-c(4,5,6) #Define Vector b > a&b [1] TRUE TRUE TRUE > a&&b [1] TRUE > a|b [1] TRUE TRUE TRUE > a||b [1] TRUE > !a [1] FALSE FALSE FALSE > !b [1] FALSE FALSE FALSE

Miscellaneous Operators:
: –> Creates sequence of numbers.
%*% –> Matrix product, binary
%o% –> Outer product, binary
%x% –> Kronecker product, binary
%in% –> Matching operator, binary (in model
formulae: nesting)

Examples:

> a<-c(1,2,3) #Define Vector a > b<-c(4,5,6) #Define Vector b > 1:8 [1] 1 2 3 4 5 6 7 8 > c=1:10 > c [1] 1 2 3 4 5 6 7 8 9 10

Powered by WordPress