ratios: Ratios of Subsequent Elements in a Numeric Vector

Description Usage Arguments Value Note Author(s) References See Also Examples

Description

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

Usage

1
ratios(x)

Arguments

x

Numeric vector.

Value

Numeric vector.

Note

This function uses C++ code to achieve a 1.5-2 times speed increase compared to the base R code: len <- length(prices); prices[2: len] / prices[1: (len - 1)].

Author(s)

Dane R. Van Domelen

References

Acknowledgment: This material is based upon work supported by the National Science Foundation Graduate Research Fellowship under Grant No. DGE-0940903.

See Also

diffs, pdiffs, pchanges

Examples

1
2
3
4
5
6
7
8
9
# Randomly generate 10 values from a standard normal distribution
x <- rnorm(10)

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

# View results
x
y

Example output

Loading required package: rbenchmark
Loading required package: quantmod
Loading required package: xts
Loading required package: zoo

Attaching package: 'zoo'

The following objects are masked from 'package:base':

    as.Date, as.Date.numeric

Loading required package: TTR
Version 0.4-0 included new data defaults. See ?getSymbols.
 [1] -0.1149643 -0.6784881 -0.2858641 -0.9166020  1.1780163  1.8047989
 [7] -0.3740372  1.0690101 -0.3006783 -0.2770259
[1]  5.9017301  0.4213252  3.2064258 -1.2851993  1.5320662 -0.2072459 -2.8580314
[8] -0.2812680  0.9213364

stocks documentation built on May 2, 2019, 5:22 p.m.