Last active
January 31, 2018 13:49
-
-
Save DavisVaughan/c2eb8d8f449a34bab10203bf3ffac01e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tibbletime) | |
library(dplyr) | |
library(zoo) | |
data(FB) | |
window <- 6 | |
rolling_mean <- rollify(mean, window = window) | |
FB_roll <- FB %>% | |
mutate( | |
# Using rollmean, with na.pad and align = "center" | |
rollmeaned = rollmean(adjusted, k = window, na.pad = TRUE, align = "center"), | |
# Rollify then Lead it | |
rollified = rolling_mean(adjusted) %>% lead(n = round(window/2)) | |
) | |
FB_roll %>% select(date, adjusted, rollmeaned, rollified) | |
#> # A tibble: 1,008 x 4 | |
#> date adjusted rollmeaned rollified | |
#> <date> <dbl> <dbl> <dbl> | |
#> 1 2013-01-02 28.0 NA NA | |
#> 2 2013-01-03 27.8 NA NA | |
#> 3 2013-01-04 28.8 28.9 28.9 | |
#> 4 2013-01-07 29.4 29.5 29.5 | |
#> 5 2013-01-08 29.1 30.1 30.1 | |
#> 6 2013-01-09 30.6 30.5 30.5 | |
#> 7 2013-01-10 31.3 30.6 30.6 | |
#> 8 2013-01-11 31.7 30.8 30.8 | |
#> 9 2013-01-14 31.0 30.7 30.7 | |
#> 10 2013-01-15 30.1 30.4 30.4 | |
#> # ... with 998 more rows | |
FB_roll %>% | |
summarise(`Equal?` = all.equal(rollmeaned, rollified)) | |
#> # A tibble: 1 x 1 | |
#> `Equal?` | |
#> <lgl> | |
#> 1 T | |
#' Created on 2018-01-31 by the [reprex package](http://reprex.tidyverse.org) (v0.1.1.9000). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note that something like
window = 7
doesn't give the same results.zoo
shifts up one more row than we do there. Not sure why as I feel like with a window of 7 the center point is clearly to place it at row 4 (3 on either side).