Last active
January 6, 2018 18:26
-
-
Save DavisVaughan/e6a9e73ac95a1acf21510e0e739e17ab 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
``` r | |
library(tibbletime) | |
library(dplyr) | |
options(digits.secs = 6) | |
# 1000 values | |
ex <- create_series('2013' ~ '2013-01-01 00:00:00.999', period = "1 millisec") | |
# Random data | |
ex$numbers <- rnorm(nrow(ex)) | |
ex | |
#> # A time tibble: 1,000 x 2 | |
#> # Index: date | |
#> date numbers | |
#> <dttm> <dbl> | |
#> 1 2013-01-01 00:00:00 1.70 | |
#> 2 2013-01-01 00:00:00 -0.902 | |
#> 3 2013-01-01 00:00:00 -0.199 | |
#> 4 2013-01-01 00:00:00 -2.27 | |
#> 5 2013-01-01 00:00:00 0.143 | |
#> 6 2013-01-01 00:00:00 -0.485 | |
#> 7 2013-01-01 00:00:00 0.304 | |
#> 8 2013-01-01 00:00:00 -0.101 | |
#> 9 2013-01-01 00:00:00 0.112 | |
#> 10 2013-01-01 00:00:00 2.14 | |
#> # ... with 990 more rows | |
ex_mean <- ex %>% | |
mutate(date = collapse_index(date, "15 millisec")) %>% | |
group_by(date) %>% | |
summarise(mean_num = mean(numbers)) | |
ex_mean | |
#> # A time tibble: 67 x 2 | |
#> # Index: date | |
#> date mean_num | |
#> <dttm> <dbl> | |
#> 1 2013-01-01 00:00:00 -0.0912 | |
#> 2 2013-01-01 00:00:00 0.0876 | |
#> 3 2013-01-01 00:00:00 -0.306 | |
#> 4 2013-01-01 00:00:00 -0.211 | |
#> 5 2013-01-01 00:00:00 -0.0460 | |
#> 6 2013-01-01 00:00:00 0.366 | |
#> 7 2013-01-01 00:00:00 -0.241 | |
#> 8 2013-01-01 00:00:00 0.345 | |
#> 9 2013-01-01 00:00:00 -0.0739 | |
#> 10 2013-01-01 00:00:00 -0.0801 | |
#> # ... with 57 more rows | |
# Check against xts | |
library(xts) | |
ex_xts <- xts(ex$numbers, ex$date) | |
ex_xts_mean <- period.apply(ex_xts, endpoints(ex_xts, "milliseconds", 15), mean) | |
head(ex_xts_mean) | |
#> Warning: timezone of object (UTC) is different than current timezone (). | |
#> [,1] | |
#> 2013-01-01 00:00:00.013 -0.09121474 | |
#> 2013-01-01 00:00:00.029 0.08757511 | |
#> 2013-01-01 00:00:00.043 -0.30586630 | |
#> 2013-01-01 00:00:00.059 -0.21118297 | |
#> 2013-01-01 00:00:00.073 -0.04603550 | |
#> 2013-01-01 00:00:00.088 0.36589842 | |
identical(as.vector(coredata(ex_xts_mean)), ex_mean$mean_num) | |
#> [1] TRUE | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment