growth_rate: Growth rate

Description Usage Arguments Value Examples

View source: R/tq_gr.R

Description

Growth rate

Usage

1
growth_rate(x, n = 12, annualize = 1, pct = TRUE, order_by = NULL)

Arguments

x

A numeric vector.

n

A positive integer indicating lag.

annualize

A positive integer indicating the number to annualize.

pct

A logical. If TRUE (default), returns percent growth rate.

order_by

A vector to order by.

Value

A numeric vector of the same length as 'x'.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
## Not run: 
# example from base
growth_rate(1:10, 4)
growth_rate(1:10, 1, annualize = 4)
# Use order_by if data not already ordered (example from dplyr)
library(dplyr, warn.conflicts = FALSE)
tsbl <- tsibble(year = 2000:2005, value = (0:5)^2, index = year)
scrambled <- tsbl %>% slice(sample(nrow(tsbl)))

wrong <- mutate(scrambled, gr = growth_rate(value))
arrange(wrong, year)

right <- mutate(scrambled, gr = growth_rate(value, order_by = year))
arrange(right, year)

## End(Not run)

mitsuoxv/tqr documentation built on Nov. 14, 2021, 7:18 p.m.