# Addition
5 + 3
[1] 8
# Subtraction
10 - 4
[1] 6
# You can chain operations
15 + 7 - 3
[1] 19
One of the best ways to start learning R is to use it as a sophisticated calculator. R excels at mathematical operations and provides many built-in functions that go far beyond what a standard calculator can do. In this lesson, we’ll explore R’s mathematical capabilities and start building your confidence with the R console.
Open RStudio and look for the Console pane (usually in the bottom-left). You’ll see a prompt that looks like this:
R version 4.5.1 (2025-06-13) -- "Great Square Root"
Copyright (C) 2025 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
>
This prompt is where you can type R commands and see immediate results.
Let’s start with fundamental arithmetic operations:
# Addition
5 + 3
[1] 8
# Subtraction
10 - 4
[1] 6
# You can chain operations
15 + 7 - 3
[1] 19
# Multiplication
6 * 7
[1] 42
# Division
20 / 4
[1] 5
# Combined operations
3 * 4 + 2
[1] 14
# Using ^ operator
2^3
[1] 8
# Using ** operator (alternative)
2**3
[1] 8
# Square root (special case)
sqrt(16)
[1] 4
# nth root using exponentiation
8^(1/3) # cube root of 8
[1] 2
# Modulo (remainder after division)
17 %% 5
[1] 2
# Integer division (quotient without remainder)
17 %/% 5
[1] 3
R follows standard mathematical order of operations (PEMDAS):
# Without parentheses
2 + 3 * 4
[1] 14
# With parentheses to change order
2 + 3) * 4 (
[1] 20
# Complex expression
2^3 + 4 * (5 - 2) / 6
[1] 10
R includes many built-in mathematical functions:
# Absolute value
abs(-5)
[1] 5
# Rounding functions
round(3.14159, 2) # Round to 2 decimal places
[1] 3.14
ceiling(3.2) # Round up
[1] 4
floor(3.9) # Round down
[1] 3
# Sign function
sign(-42) # Returns -1, 0, or 1
[1] -1
# Natural logarithm
log(10)
[1] 2.302585
# Logarithm base 10
log10(100)
[1] 2
# Logarithm with custom base
log(8, base = 2)
[1] 3
# Exponential function
exp(1) # e^1
[1] 2.718282
# Power of 10
10^2
[1] 100
# Basic trigonometric functions (in radians)
sin(pi/2)
[1] 1
cos(0)
[1] 1
tan(pi/4)
[1] 1
# Inverse functions
asin(1)
[1] 1.570796
acos(1)
[1] 0
atan(1)
[1] 0.7853982
# Convert degrees to radians
<- function(degrees) degrees * pi / 180
degrees_to_radians sin(degrees_to_radians(90))
[1] 1
R has several built-in constants:
# Pi
pi
[1] 3.141593
# Euler's number
exp(1)
[1] 2.718282
# Infinity
Inf
[1] Inf
1/0 # Also gives Inf
[1] Inf
# Not a Number
NaN
[1] NaN
0/0 # Results in NaN
[1] NaN
# Not Available (missing value)
NA
[1] NA
One of R’s superpowers is vectorized operations:
# Using c() function to combine values
<- c(1, 2, 3, 4, 5)
numbers numbers
[1] 1 2 3 4 5
# Creating sequences
1:10 # Numbers 1 through 10
[1] 1 2 3 4 5 6 7 8 9 10
seq(0, 20, by = 2) # Even numbers from 0 to 20
[1] 0 2 4 6 8 10 12 14 16 18 20
# Operations on entire vectors
<- c(1, 2, 3, 4)
x <- c(5, 6, 7, 8)
y
# Element-wise operations
+ y x
[1] 6 8 10 12
* y x
[1] 5 12 21 32
^2 x
[1] 1 4 9 16
# Operations with single numbers (recycling)
+ 10 x
[1] 11 12 13 14
* 2 x
[1] 2 4 6 8
# Create some data
<- c(85, 92, 78, 96, 88, 75, 89)
scores
# Summary statistics
sum(scores) # Total
[1] 603
mean(scores) # Average
[1] 86.14286
median(scores) # Middle value
[1] 88
max(scores) # Maximum
[1] 96
min(scores) # Minimum
[1] 75
range(scores) # Min and max
[1] 75 96
length(scores) # Number of elements
[1] 7
# More advanced statistics
var(scores) # Variance
[1] 55.80952
sd(scores) # Standard deviation
[1] 7.470577
R can also perform logical operations:
# Equal to
5 == 5
[1] TRUE
# Not equal to
5 != 3
[1] TRUE
# Greater than / Less than
10 > 5
[1] TRUE
3 < 7
[1] TRUE
# Greater than or equal to / Less than or equal to
5 >= 5
[1] TRUE
4 <= 3
[1] FALSE
# AND operator
TRUE & TRUE
[1] TRUE
5 > 3) & (2 < 4) (
[1] TRUE
# OR operator
TRUE | FALSE
[1] TRUE
5 > 10) | (2 < 4) (
[1] TRUE
# NOT operator
!TRUE
[1] FALSE
!(5 > 10)
[1] TRUE
Let’s work through some practical calculation examples:
# Calculate compound interest
# Formula: A = P(1 + r/n)^(nt)
<- 1000 # Initial amount
principal <- 0.05 # 5% annual interest rate
rate <- 12 # Compounded monthly
n <- 5 # 5 years
t
<- principal * (1 + rate/n)^(n*t)
final_amount final_amount
[1] 1283.359
# Interest earned
<- final_amount - principal
interest_earned interest_earned
[1] 283.3587
# BMI calculation: weight (kg) / height (m)^2
<- 70
weight_kg <- 1.75
height_m
<- weight_kg / height_m^2
bmi round(bmi, 1)
[1] 22.9
# BMI for multiple people
<- c(65, 70, 80, 55)
weights <- c(1.70, 1.75, 1.80, 1.60)
heights <- weights / heights^2
bmis round(bmis, 1)
[1] 22.5 22.9 24.7 21.5
# Celsius to Fahrenheit: F = (C * 9/5) + 32
<- c(0, 20, 30, 37, 100)
celsius_temps <- (celsius_temps * 9/5) + 32
fahrenheit_temps fahrenheit_temps
[1] 32.0 68.0 86.0 98.6 212.0
# Fahrenheit to Celsius: C = (F - 32) * 5/9
<- c(32, 68, 86, 98.6, 212)
fahrenheit_example <- (fahrenheit_example - 32) * 5/9
celsius_converted celsius_converted
[1] 0 20 30 37 100
# R uses scientific notation for very large or small numbers
1000000
[1] 1e+06
0.0001
[1] 1e-04
# You can control the display
options(scipen = 999) # Avoid scientific notation
1000000
[1] 1000000
options(scipen = 0) # Reset to default
# This just calculates and displays the result
5 + 3
[1] 8
# This calculates and stores the result in a variable
<- 5 + 3
result
# To see the stored value, type the variable name
result
[1] 8
# Or use parentheses to assign and display
<- 10 * 2) (result2
[1] 20
# R is case-sensitive
# This will give an error
PI # This works pi
# Incorrect
c(1, 2, 3) # Missing parentheses - ERROR
mean
# Correct
mean(c(1, 2, 3))
# Be careful with division
5 / 0 # Returns Inf
[1] Inf
0 / 0 # Returns NaN
[1] NaN
Try these calculations on your own:
In this lesson, you’ve learned to use R as a powerful calculator that can:
Now that you’re comfortable with R’s calculation capabilities, let’s explore:
Comments in R
Notice the use of
#
for comments in the examples above. Comments are crucial for explaining your code: