The arithmetic operators in R
are used to perform mathematical operations. The most common arithmetic operators, alongside their names and examples, are shown in the table below:
Operator | Name | Examples |
---|---|---|
+ |
Addition operator: Takes the sum of two operands | 3 + 2 = 5 |
- |
Subtraction operator: Subtracts the right operand from the left operand | 3 - 2 = 1 |
* |
Multiplication operator: Takes the multiplication of the operands | 3 * 2 = 6 |
/ |
Division operator: Takes the division of the operands | 6 / 2 = 3 |
^ |
Exponential: Takes the power of an operand against the other | 3 ^ 2 = 9 |
%% |
Modulus operator: Returns the remainder integer after the division of the operands | 3 %% 2 = 1 |
%/% |
Integer division operator: Returns the integer part of a division and does away with the decimal part | 16 % / % 3 = 5 |
Let’s write a code to include all the assignment operators highlighted in the table above:
x <- 16 y <- 3 paste('x = ', x ) paste('y = ', y) # Using the addition operator paste('x + y = ', x + y) # using the subtraction operator paste('x - y = ', x - y) # using the multiplication operator paste('x * y = ', x * y) # using the division operator paste('x / y = ', x / y) # using the exponential operator paste('x ^ y = ', x ^ y) # using the modulus operator paste('x %% y = ', x %% y) # using the integer division operator paste('x %/% y = ', x %/% y)
RELATED TAGS
CONTRIBUTOR
View all Courses