---
title: "Motivation"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{motivation}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
The problem arises when we have to use time-series in a data.frame or use time-series
operations like `lag` and `diff` for numeric vectors in a data.frame. Let's look into
an example.
```{r setup}
library(transx)
```
First, wee restrict tibble printing options to minimize the space occupied.
```{r problem, message=FALSE}
library(dplyr)
options(tibble.print_min = 3)
```
Let's load the economics dataset from `ggplot2` for illustration.
```{r}
econ <- ggplot2::economics
econ
```
Then, we are going to use some `stats` functions:
```{r exist-already, error=TRUE}
mutate(econ, pop_lag = stats::lag(as.ts(pop)))
```
`base::lag` only works on `ts` objects. However, dplyr has thought about this problem
```{r dplyr-lag}
mutate(econ, pop_lag = dplyr::lag(pop))
```
However, this problem extends to all the univariate functions that are applied in
the same manner in a data.frame. For example
```{r example-diff, error=TRUE}
mutate(econ, pop_diff = base::diff(pop))
```
The idea for `transx` is coming from the need to construct wrapper functions.
```{r new-fun}
diffx <- function(x, ...) x - dplyr::lag(x, ... )
mutate(econ, pop_diff = diffx(pop))
```