Learn R Programming

stocks (version 1.1.4)

ratios: Ratios of Subsequent Elements in a Vector

Description

Calculates vector of ratios of a vector, i.e. ratio of x[2] to x[1], ratio of x[3] to x[2], and so forth.

Usage

ratios(x)

Arguments

x

Numeric vector.

Value

Numeric vector.

Examples

Run this code
# NOT RUN {
# Generate 10 values from N(0, 1)
x <- rnorm(10)

# Calculate vector of ratios
(y <- ratios(x))

# Slower base R computation
len <- length(x)
y2 <- x[2: len] / x[1: (len - 1)]
all.equal(y, y2)


# }

Run the code above in your browser using DataLab