How To Do Arithmetic Operations in R?
We can do arithmetic operations on vectors such as addition, subtraction, multiplication and division. To perform arithmetic operations of vectors, it is required that they should have the same length and the same type.
Example – Addition and Subtraction:
#first, we create vector m and n having integer values
m <- c(100, 200, 300, 400, 500)
n <- c(20, 40, 60, 80, 90)
result1 <- m + n
result2 <- m-n
print(result1)
print(result2)
#output:
[1] 120 240 360 480 590
[1] 80 160 240 320 410
Example – Multiplication and Division
#first, we create vector m and n having integer values
m <- c(100, 200, 300, 400, 500)
n <- c(20, 40, 60, 80, 100)
result1 <- m * n
result2 <- m/n
print(result1)
print(result2)
#output:
[1] 2000 8000 18000 32000 50000
[1] 5 5 5 5 5
Conclusion:
We can do arithmetic operations like addition, subtraction, multiplication, division on vectors.
