Section 26 Vector Operation: Mathematical Function

26.1 Arithmetic Function

Function Explanation
round Round to given number of decimal places
trunc Truncate down to nearest whole number
ceiling, floor Round values in a vector up (ceiling) or down (floor)
zapsmall Replace values in a vector or matrix that are close to zero with 0
abs Absolute (unsigned) value
sqrt Square root
exp Exponential
log, log10, log2 Log to base e, 10, and 2
sin, cos, tan Trigonometric functions
asin, acos, atan Inverse (arc) trigonometric functions
scale Centering and scaling

26.2 Example 1

26.2.1 Elementwise operation on a vector

x <- c(0, 2, 4, 6)

x+5

x-5

x*4

x/2

x^2

-x

sqrt(x)

exp(x)

scale(x)

26.3 Example 2

26.3.1 Both vectors are of the same length

x <- c(0, 2, 4, 6)
y <- c(4, -2, 7, 9)
x+y
x-y
x/y
x*y
exp(x) + exp(y)
sqrt(x) + sqrt(y)

26.4 Example 3

26.4.1 The longer vector is a multiple of the shorter vector

x <- c(0, 2, 4, 6)
y <- c(4, -2)
x+y
x-y
x/y
x*y

26.5 Example 4

26.5.1 The longer vector is NOT a multiple of the shorter vector

x <- c(0, 2, 4, 6)
y <- c(4, -2, 7)
x+y
x-y
x/y
x*y