Basic Math Functions In R

Want to learn more? I recommend working through: R for Data Science, R Cookbook, and R Graphics Cookbook.

Arithmetic operators

# 1 plus 1
1 + 1
[1] 2
# 4 minus 3
4 - 3
[1] 1
# 14 divided by 10
14 / 10
[1] 1.4
# 10 multiplied by 5
10*5
[1] 50
# 3 squared
3^2
[1] 9
# 5 mod 2
5%%2
[1] 1
# 4 divided by 2 (integer division)
4%/%2
[1] 2
# log to the base e of 2
log(2)
[1] 0.6931472
# antilog of 2
exp(2)
[1] 7.389056
# log to base 2 of 3
log(3,2)
[1] 1.584963
# log to base 10 of 2
log10(2)
[1] 0.30103
# square root of 2
sqrt(2)
[1] 1.414214
# !5
factorial(4)
[1] 24
# largest interger smaller than 2
floor(2)
[1] 2
# smallest integer greater than 6
ceiling(6)
[1] 6
# round 3.14159 to three digits
round(3.14159, digits=3)
[1] 3.142
# create 10 random digits between zero and 1 (from a uniform distribution)
runif(10)
 [1] 0.07613962 0.66543266 0.48379168 0.40593920 0.67715428 0.49170373
 [7] 0.62351598 0.19275859 0.48018351 0.34890640
# cosine of 3
cos(3)
[1] -0.9899925
# sine of 3
sin(3)
[1] 0.14112
# tangent of 3
tan(3)
[1] -0.1425465
# absolute value of -3
abs(-3)
[1] 3