Arithmetic Operators
Part of the Fundamentals section of Coddy's R journey — lesson 12 of 78.
Arithmetic operators let you perform mathematical calculations in R. These operators work with numeric values and follow the same mathematical rules you already know.
R supports five basic arithmetic operators:
| Operator | Operation | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 15 / 3 | 5 |
^ | Exponentiation | 2 ^ 3 | 8 |
You can use these operators directly with numbers or with variables that contain numeric values:
price <- 25
quantity <- 4
total <- price * quantityR follows standard mathematical order of operations: exponentiation first, then multiplication and division, and finally addition and subtraction. Use parentheses to control the order when needed, just like in regular math.
Challenge
EasyCalculate the total cost of a shopping trip using arithmetic operators.
You are provided with the following variables:
apples <- 6
apple_price <- 1.5
milk_cartons <- 2
milk_price <- 3.25
discount <- 5Calculate and store the following values:
- Create a variable called
apples_totalthat stores the cost of all apples (quantity multiplied by price) - Create a variable called
milk_totalthat stores the cost of all milk cartons (quantity multiplied by price) - Create a variable called
subtotalthat stores the sum ofapples_totalandmilk_total - Create a variable called
final_totalthat stores the subtotal minus the discount
Use the print() function to display apples_total, milk_total, subtotal, and final_total in that exact order.
Cheat sheet
R supports five basic arithmetic operators:
| Operator | Operation | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 15 / 3 | 5 |
^ | Exponentiation | 2 ^ 3 | 8 |
You can use these operators with numbers or variables:
price <- 25
quantity <- 4
total <- price * quantityR follows standard mathematical order of operations: exponentiation first, then multiplication and division, and finally addition and subtraction. Use parentheses to control the order when needed.
Try it yourself
# Given variables
apples <- 6
apple_price <- 1.5
milk_cartons <- 2
milk_price <- 3.25
discount <- 5
# TODO: Write your code below
# 1. Calculate apples_total (quantity * price)
# 2. Calculate milk_total (quantity * price)
# 3. Calculate subtotal (sum of apples_total and milk_total)
# 4. Calculate final_total (subtotal minus discount)
# Print the results in order: apples_total, milk_total, subtotal, final_totalThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values