diff --git a/README.Rmd b/README.Rmd index c5f5e2c..ba693c1 100755 --- a/README.Rmd +++ b/README.Rmd @@ -153,7 +153,6 @@ runner( ) ``` - ### `NA` padding Using `runner` one can also specify `na_pad = TRUE` which would return `NA` for any window which is partially out of range - meaning that there is no sufficient @@ -179,6 +178,33 @@ runner( ) ``` +### Using runner with `data.frame` + +User can also put `data.frame` into `x` argument and apply functions which involve +multiple columns. In example below we calculate beta parameter of `lm` model on +1, 2, ..., n observations respectively. On the plot one can observe how `lm` +parameter adapt with increasing number of observation. + +```{r eval=FALSE} +date <- Sys.Date() + cumsum(sample(1:3, 40, replace = TRUE)) # unequaly spaced time series +x <- cumsum(rnorm(40)) +y <- 30 * x + rnorm(40) + +df <- data.frame(date, y, x) + +slope <- runner( + df, + k = 10, + idx = "date", + function(x) { + coefficients(lm(y ~ x, data = x))[2] + } +) + +plot(slope) +abline(h = 30, col = "blue") +``` + ### Build-in functions With `runner` one can use any R functions, but some of them are optimized for speed reasons. diff --git a/README.md b/README.md index c68a956..0a74c96 100755 --- a/README.md +++ b/README.md @@ -175,6 +175,34 @@ runner( ) ``` +### Using runner with `data.frame` + +User can also put `data.frame` into `x` argument and apply functions +which involve multiple columns. In example below we calculate beta +parameter of `lm` model on 1, 2, …, n observations respectively. On the +plot one can observe how `lm` parameter adapt with increasing number of +observation. + +``` r +date <- Sys.Date() + cumsum(sample(1:3, 40, replace = TRUE)) # unequaly spaced time series +x <- cumsum(rnorm(40)) +y <- 30 * x + rnorm(40) + +df <- data.frame(date, y, x) + +slope <- runner( + df, + k = 10, + idx = "date", + function(x) { + coefficients(lm(y ~ x, data = x))[2] + } +) + +plot(slope) +abline(h = 30, col = "blue") +``` + ### Build-in functions With `runner` one can use any R functions, but some of them are