Arithmetic operators help you do math with numbers in your programs. They let you add, subtract, multiply, and divide easily.
Arithmetic operators in R Programming
result <- value1 operator value2 # Operators: # + addition # - subtraction # * multiplication # / division # ^ exponentiation (power) # %% modulus (remainder) # %/% integer division
Use <- to store the result of the calculation in a variable.
Operators work between two numbers or variables holding numbers.
sum <- 5 + 3 product <- 4 * 2
difference <- 10 - 7 division <- 20 / 4
power <- 2 ^ 3 remainder <- 10 %% 3
int_div <- 17 %/% 5
This program shows how to use all main arithmetic operators with two numbers, 8 and 3. It prints each result clearly.
a <- 8 b <- 3 sum <- a + b difference <- a - b product <- a * b division <- a / b power <- a ^ b remainder <- a %% b int_div <- a %/% b print(paste("Sum:", sum)) print(paste("Difference:", difference)) print(paste("Product:", product)) print(paste("Division:", division)) print(paste("Power:", power)) print(paste("Remainder:", remainder)) print(paste("Integer Division:", int_div))
Division (/) always gives a decimal number in R, even if the result is whole.
Use integer division (%/%) when you want only the whole number part of division.
Modulus (%%) gives the leftover part after division, useful for checking if a number divides evenly.
Arithmetic operators let you do basic math in R.
Use +, -, *, / for addition, subtraction, multiplication, and division.
Use ^ for powers, %% for remainder, and %/% for integer division.
Practice
Solution
Step 1: Recall basic arithmetic operators in R
In R,*is used for multiplication,+for addition,-for subtraction, and/for division.Step 2: Identify the multiplication operator
The operator*multiplies two numbers.Final Answer:
* -> Option CQuick Check:
Multiplication operator = * [OK]
- Confusing * with + or -
- Using / for multiplication
- Using ^ for multiplication
Solution
Step 1: Understand remainder operator in R
In R, the operator%%gives the remainder after division.Step 2: Identify correct syntax for remainder
10 %% 3calculates remainder of 10 divided by 3, which is 1.Final Answer:
10 %% 3 -> Option AQuick Check:
Remainder operator = %% [OK]
- Using single % instead of %%
- Confusing integer division %/% with remainder %%
- Typing operators incorrectly like /%
result <- 5 + 3 * 2
print(result)
Solution
Step 1: Apply operator precedence in R
Multiplication (*) has higher priority than addition (+), so 3 * 2 = 6 is calculated first.Step 2: Calculate the expression
Then add 5 + 6 = 11.Final Answer:
11 -> Option BQuick Check:
5 + (3 * 2) = 11 [OK]
- Adding before multiplying
- Ignoring operator precedence
- Calculating 5 + 3 first
value <- 10 %/ 3
print(value)
Solution
Step 1: Check the operator used
The operator%/is not valid in R. Integer division uses%/%.Step 2: Identify the error type
Using%/causes a syntax error because R does not recognize it.Final Answer:
Syntax error due to incorrect operator -> Option AQuick Check:
Integer division operator is %/% not %/ [OK]
- Typing %/ instead of %/%
- Confusing remainder %% with integer division
- Expecting output despite syntax error
int_div and rem?Solution
Step 1: Recall operators for integer division and remainder
Integer division uses%/%and remainder uses%%in R.Step 2: Match operators to variables
int_div <- 17 %/% 4correctly assigns integer division result (4).rem <- 17 %% 4correctly assigns remainder (1).Final Answer:
int_div <- 17 %/% 4; rem <- 17 %% 4 -> Option DQuick Check:
Integer division %/% and remainder %% used correctly [OK]
- Swapping operators for int division and remainder
- Using invalid operator %/
- Using / instead of %/% for integer division
Ready to prove it?
Pass the test and this pattern moves unknown
