Here, we will show operations in R such as assignment, arithmetic, relational, Boolean, and logical operations with examples and notes on their usage.

These operations can be performed on different data structures such as vectors, matrices, and arrays, including dates and times.

See also operations and relations on plots.

1 Assignment Operations in R

First, we discuss the assignment operations which are performed with the commonly used left assignment operator and the less common right assignment operator.

Examples are provided in the table below.

Assignment Operators in R
Symbol Usage Examples
= Left assignment;
most used

a = 1

a

[1] 1

<- Left assignment

x <- c(2, 3)

x

[1] 2 3

<<- Left assignment;
*mostly used in functions

a <<- 4; b <<- a; b

[1] 4

-> Right assignment

2 -> x

x

[1] 2

->> Right assignment;
*mostly used in functions

c(2, 3) ->> x

x

[1] 2 3

2 Arithmetic Operations and Functions in R

For arithmetic operations, these are performed with addition, subtraction, multiplication, division, exponentiation operators and some of the less common ones.

Examples are provided in the table below.

Arithmetic Operators and Functions in R
Symbol Usage Examples
+ Addition

2 + 3

[1] 5

- Subtraction

x = 7

y = 3

x - y

[1] 4

* Multiplication

x = 2; y = 5; x*y

[1] 10

/ Division

a = c(7, 3); b = c(3, 2)

a/b

[1] 2.333333 1.500000

^ Exponentiation or power

2^10

[1] 1024

** Exponentiation or power

5**2

[1] 25

x%/%y Integer division of \(x\) by \(y\)

7%/%2

[1] 3

x%%y

\(x\) Modulo \(y\)

For remainder after division

7%%2

[1] 1

sqrt(x)

Square root of \(x\)

Returns positive number

sqrt(25)

[1] 5

abs(x) Absolute value of \(x\)

abs(-4.5)

[1] 4.5

exp(x) Exponentiation of the number
\(e\) \((=2.718282)\) by \(x\)

exp(1)

[1] 2.718282

log(x, n)

Logarithm of \(x\), base \(n\)

*Default \(n=e\)

log(8, 2); log(exp(1))

[1] 3

[1] 1

floor(x) Largest integer less than \(x\)

floor(5.6); floor(-5.6)

[1] 5

[1] -6

ceiling(x) Smallest integer greater than \(x\)

ceiling(6.4); ceiling(-6.4)

[1] 7

[1] -6

round(x, n)

Rounds \(x\) to \(n\) decimal places

*Default \(n=0\)

round(5.7306, 2); round(5.7306)

[1] 5.73

[1] 6

signif(x, n) Rounds \(x\) to \(n\) significant digits

signif(5.7306, 2)

[1] 5.7

trunc(x)

Integer part of \(x\)

trunc(5.6); trunc(-5.6)

[1] 5

[1] -5

x%%1

abs(x)%%1

Decimal part of \(x\)

4.6%%1; abs(-4.6)%%1

[1] 0.6

[1] 0.6

sin(x) Sine of \(x\), with angles
in radians (\(pi=3.141593\))

sin(pi/2)

[1] 1

cos(x) Cosine of \(x\), with angles
in radians

cos(0)

[1] 1

tan(x) Tangent of \(x\), with angles
in radians (\(pi=3.141593\))

tan(pi/4)

[1] 1

asin(x) Inverse sine of \(x\),
(\(pi/2=1.570796\))

asin(1)

[1] 1.570796

acos(x) Inverse cosine of \(x\)

acos(1)

[1] 0

atan(x) Inverse tangent of \(x\),
(\(pi/4=0.7853982\))

atan(1)

[1] 0.7853982

Note the following:

  • When a function has a default value, the default value does not need to be specified, and that argument can be excluded. For example, log(x, e) = log(x) since \(e\) is the default value of \(n\). Similarly, round(2.1, 0) = 2 = round(2.1) since \(0\) is the default value of \(n\).
  • The function round(x, n) rounds 0.5 to an even number when set to \(0\) decimal places. For example round(5.5, 0) = 6 and round(6.5, 0) = 6.

3 Relational or Comparison Operations in R

For relational or comparison operations that return TRUE or FALSE values that are performed with greater than, lesser than, equal to and not equal to relational operators.

Examples are provided in the table below.

Relational or Comparison Operators in R
Symbol Usage Examples
> Greater than?

2 > 3

[1] FALSE

< Lesser than? x = 3
y = 7

x < y

[1] TRUE

>= Greater than or equal to?

x = 2; y = 5; x >= y

[1] FALSE

<= Lesser than or equal to?

a = c(7, 3, 2); b = c(3, 5, 2)

a <= b

[1] FALSE TRUE TRUE

== Equal to?

2^10 == 1024

[1] TRUE

!= Not equal to?

c(7, 3, 5) != c(2, 3, 5)

[1] TRUE FALSE FALSE

Note the following:

  • 2.001-2 will return 0.001 but 2.001-2 == 0.001 will return FALSE. This is not intuitive; however, it is because in R, numbers including floating points are represented in binary format which leads to some numerical errors. These errors are approximated away when converted back to decimal format. To highlight this binary conversion error: print(2.001-2, digits = 16) returns 0.0009999999999998899 not 0.001.

4 Logical or Boolean Operations in R

For logical or Boolean operations that return TRUE or FALSE values that are performed with AND, OR, and negation logical operators.

Examples are provided in the table below.

Logical or Boolean Operators in R
Symbol Usage Examples
x | y

x OR y:

Checks element-wise in vector.
TRUE if at least one side is TRUE

c(4==2^2, FALSE) | c(pi>2, 2==3)

[1] TRUE FALSE

x || y

x Logical OR y:

Checks two statements.
TRUE if at least one side is TRUE

5==2 || 2==3

[1] FALSE

x & y

x AND y:

Checks element-wise in vector.
TRUE if both sides are TRUE

c(FALSE, pi>2) & c(2<=3, !FALSE)

[1] FALSE TRUE

x && y

x Logical AND y:

Checks two statements.
TRUE if both sides are TRUE

2==2 && 2<=3

[1] TRUE

!x Not x or negation logic of x

!c(7 < 3,TRUE, FALSE)

[1] TRUE FALSE TRUE

xor(x, y) Is at least one of x or y true?

xor(!TRUE, 5>=4)

[1] TRUE

isTRUE(x) Is x true?

isTRUE(2^10 == 1024)

[1] TRUE

isFALSE(x) Is x false?

isFALSE(2^10 == 1024)

[1] FALSE

5 Other Operations in R

Besides the operations and operators above, there are two commonly used ones, the colon operator, and the value matching operator.

Examples are provided in the table below.

Colon and Value Matching Operators in R
Symbol Usage Examples
a:b Returns numbers from a to b

1:7

[1] 1 2 3 4 5 6 7

x %in% y Checks for matches of x in y

c(5, 7) %in% c(2, 3, 5)

[1] TRUE FALSE

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes