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 <- 16y <- 3paste('x = ', x )paste('y = ', y)# Using the addition operatorpaste('x + y = ', x + y)# using the subtraction operatorpaste('x - y = ', x - y)# using the multiplication operatorpaste('x * y = ', x * y)# using the division operatorpaste('x / y = ', x / y)# using the exponential operatorpaste('x ^ y = ', x ^ y)# using the modulus operatorpaste('x %% y = ', x %% y)# using the integer division operatorpaste('x %/% y = ', x %/% y)