In this tutorial you will learn about the arithmetic operators in R. Like most of the programming languages, R programming also has arithmetic operators to perform arithmetic operations.
Arithmetic Operators in R
Table below shows the list of symbols used as arithmetic operators in R programming language. All these arithmetic operators are binary operators, means they operate on two operands.
Operator Symbol | Arithmetic Operation | Examples |
---|---|---|
$+$ | Unary plus | + x |
$-$ | Unary minus | - x |
$+$ | Addition | x + y |
$-$ | Subtraction | x - y |
$\ast$ | Multiplication | x * y |
$/$ | Division | x / y |
$\hat{}$ or $\ast\ast$ | Raise to power (exponentiation) | x ^ y |
$\%/\%$ | Integer division | x %/% y |
$\%\%$ | Remainder from integer division (Modulus) | x %% y |
Examples of Arithmetic Operators
Addition
Suppose we have two variables x
and y
with respective values 29 and 4.
# Assign the value 29 to variable x
x <- 29
# Assign the value 4 to variable y
y <- 4
# Add the values x and y and display result
x + y
[1] 33
The first statement assign the value 29 to the variable x
and second statement assign the value 4 to the variable y
. The third line of code perform addition of two variables x
and y
and display the result.
Subtraction
# Subtract y from x and store in subt
subt <- x - y
# Print the value of subt
subt
[1] 25
The first line of code subtract y
from x
and store the result in subt
. The second line of code display the value of subt
on the screen.
Multiplication
Suppose the height and width of the rectangle is height=4
cm and width=6
cm. Then the area of rectangle is Area = height*width
. The following R code create a variable height
and width
. Compute the area of a rectangle and store the result in Area.rect
.
# Assign 4 to height
height <- 4
# Assign6 to weight
width <- 6
# Multiply the height by width and store in Area.rect
Area.rect <- height * width
# Print the value of Area.rect
Area.rect
[1] 24
Division
Suppose we have two variables x
and y
with respective values 29 and 4.
# Assign the value 29 to variable x
x <- 29
# Assign the value 4 to variable y
y <- 4
# Divide the value x by y and store in div
div <- x/y
# Print the value of div
div
[1] 7.25
Above R code create a variable x
and y
with respective values 29 and 4. Next line of R code divide x
by y
and store the result in div
.
Exponentiation
Suppose the radius of the circle is radius = 5
cm. The following R code create a variable radius
and compute the area of a circle with radius = 5
cm.
# Assign 5 to radius
radius<-5
# compute area of circle
area.circle <-pi*radius^2
# Print the value of area.circle
area.circle
[1] 78.53982
Note that pi
is a built-in constant in R which is 22/7=3.141593..
.
Check built-in constant.
Integer Division
Suppose we have two variables x
and y
with respective values 29 and 4.
# Assign the value 29 to variable x
x <- 29
# Assign the value 4 to variable y
y <- 4
# integer division of x by y and store in integer_div
integer_div <- x %/% y
# Print the value of integer_div
integer_div
[1] 7
Above R code perform integer division of x
by y
(i.e., 29 by 4) and the result is stored in integer_div
. The second line of code display the value of integer_div
on the screen.
Remainder from Integer Division
Suppose we have two variables x
and y
with respective values 29 and 4.
# Assign the value 29 to variable x
x <- 29
# Assign the value 4 to variable y
y <- 4
# remainder from integer division of x by y
modulus <- x %% y
# print the value of modulus
modulus
[1] 1
The first line of code perform integer division of x
by y
and store the result of the remainder in modulus
. The second line of code display the value of modulus
on the screen.
# integer division of 16 by 4 and display remainder
16 %% 4
[1] 0
In the above code as 16 is completely divisible by 4, the remainder will be 0.
Arithmetic operations on Vector and scalar
R performs vectorized computations. Vectorized computations is any arithmetic computation that when applied to a vector operates on all the elements of vector. That is arithmetic operations on vectors are performed element-wise.
To perform arithmetic operations between a vector and a scalar, R uses Recycling Rule.
x<-c(1,2,3,4)
x
[1] 1 2 3 4
The first line of code combine the arguments 1,2,3,4
to a vector x
. The second line of code display the result.
Remember that the c()
function is known as **concatenation** or **combine** which combines its arguments.
Addition
In the following R code, x
is a vector and 5
is a scalar. To add 5
to x
, using recycling rule R adds 5
to every elements in x
.
# 5 is added to each element of x
x + 5
[1] 6 7 8 9
Subtraction
# 2 is subtracted from each element of x
x - 2
[1] -1 0 1 2
From every elements in vector x
subtract the scalar 2
and display the result as a vector.
Multiplication
# each element of x is multiplied by 2
x * 2
[1] 2 4 6 8
Every elements in vector x
is multiplied by scalar 2
and display the result as a vector.
Division
# each element of x is divided by 2
x / 2
[1] 0.5 1.0 1.5 2.0
Every elements in vector x
is divided by scalar 2
and display the result as a vector.
Integer Division
# integer division on dividing each element by 2
x %/% 2
[1] 0 1 1 2
Perform integer division on every elements in vector x
by scalar 2
and display the result as a vector.
Remainder from Integer Division
# Remainder from integer division
x %% 2
[1] 1 0 1 0
Perform integer division on every elements in vector x
by scalar 2
and display the result as a vector.
Exponentiation
# Exponentiation
x ^ 2
[1] 1 4 9 16
Perform the power of scalar 2
on every element in vector x
and display the result as a vector.
Arithmetic operations on Vectors of unequal length
As R perform vectorized arithmetic operations, some problems arises while performing arithmetic operations on two vectors of unequal length. In such a situation R display a warning message but perform arithmetic operations using recycling rule. The recycling rule states that the shorter vector is replicated enough number of times so that the results has the length of the longer vector.
x<-c(1,2,3,4)
y<-c(5,6,7)
Addition
x + y
Warning in x + y: longer object length is not a multiple of shorter object
length
[1] 6 8 10 9
As y
is a shorter vector than x
, R recycled the elements of y
as (5,6,7,5)
and then perform element-wise addition.
Subtraction
x - y
Warning in x - y: longer object length is not a multiple of shorter object
length
[1] -4 -4 -4 -1
As y
is a shorter vector than x
, R recycled the elements of y
as (5,6,7,5)
and then perform element-wise subtraction.
All other arithmetic operations uses similar recycling rule to perform arithmetic operations.
Endnote
In this tutorial you learned about arithmetic operators in R and how R uses recycling rule while performing arithmetic operations.
To learn more about other operators in R, please refer to the following tutorials:
Assignment operators
Relational operators
Logical operators in R
Miscellaneous operators in R
Precendence of Operators in R
Operators in R
Hopefully you enjoyed learning this tutorial on arithmetic operators in R. Hope the content is more than sufficient to understand arithmetic operators in R.