One of R’s most fundamental uses is as a calculator. R can run any number of mathematical functions. The following chunk shows the basics.

Simple operations

Simple mathematical operations can be performed easily

2+2 # Addition via the + symbol
4-2 # Subtraction via the - symbol
2*2 # Multiplication via the * symbol
2/2 # Division via the / symbol
2^2 # Exponents via the ^ symbol
## [1] 4
## [1] 2
## [1] 4
## [1] 1
## [1] 4

R follows PEMDAS rules as well.

2*3+100 
100 + 2*3
(100 +2)*3
## [1] 106
## [1] 106
## [1] 306

Simple functions

R also contains a wide variety of functions to perform different mathematical operations.

#Here are a few examples
sum(2,2,3,5,6,8) #Sum of all these numbers
mean(2,2,3,5,6,8) #The average of all these numbers
median(2,2,3,5,6,8) #The median of these numbers
sqrt(81) #get the square root of a number
sd(c(2,3,4,5,6,7,8,2,10,10)) #The standard deviation of a a set of numbers
## [1] 26
## [1] 2
## [1] 2
## [1] 9
## [1] 3.020302

We can also round values using ceiling() (to round up) and floor() (to round down). The round() function exists as well and follows standard rounding rules.

x <- 3.2
ceiling(x)
floor(x)
round(x)
## [1] 4
## [1] 3
## [1] 3
Last modified: Tuesday, 2 December 2025, 4:37 AM