R - Math Functions


R Code

Examples

Function

a + b
> 3+4
[1] 7

Addition

a - b

> 10-5
[1] 5

Subtraction

a * b

> 6*4
[1] 24

Multiplication

a / b

> 50/10
[1] 5

Division

2 + 4 * 5

> 6+10/5
[1] 8

Order of Operations

abs(a-b)

> abs(3-8)
[1] 5

Absolute Value

factorial(x)

> factorial(4)
[1] 24

Factorial of x (x!)

x^n

> 2^3
[1] 8

x raised to the nth power

xen

> 3e2
[1] 300

x * 10n

log10(x)

> log10(100)
[1] 2

Common logarithm of x with base 10

log(x, n)

> log(16,4)
[1] 2

Logarithm of x with base n

log(x)

> log(42)
[1] 3.73767

Natural logarithm with base e (2.1782)

exp(n)

> exp(7)
[1] 1096.633

Exponential function: en

sqrt(x)

> sqrt(49)
[1] 7

Square root of x

floor(x)

> floor(3.77)
[1] 3

Rounds x down

ceiling(x)

> ceiling(3.44)
[1] 4

Rounds x up

pi

> pi
[1] 3.141593

3.141593

round(x, n)

> round(3.575757575, 3)
[1] 3.576

Round a number x to n decimal places

round(pi, 0)

> round(pi, 0)
[1] 3

Round pi to a whole number

round(pi, n)

> round(pi, 4)
[1] 3.1416

Round pi to n decimal places

cos(x)

> cos(0.5)
[1] 0.8775826

Cosine of x

sin(x)

> sin(0.5)
[1] 0.4794255

Sine of x

tan(x)

> tan(0.5)
[1] 0.5463025

Tangent of x

acos(x)

> acos(1)
[1] 0

Inverse cosine (arccosine)of x

asin(x)

> asin(1)
[1] 1.570796

Inverse sine (arcsine) of x

atan(x)

> atan(1)
[1] 0.7853982

Inverse tangent (arctangent) of x

 

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.