Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- library(rlang)
- library(dplyr)
- library(tibbletime)
- library(purrr)
- data(FB)
- # ------------------------------------------------------------------------
- # Call mutate, but with the lhs being names and the rhs being expressions
- mutate_double_sided <- function(.data, x) {
- dot_equal <- enquo(x)
- dot_expr <- quo_get_expr(dot_equal)
- dot_env <- quo_get_env(dot_equal)
- lhs <- dot_expr[[2]]
- rhs <- dot_expr[[3]]
- lhs_quos <- lapply(lhs, function(x) new_quosure(x))[-1]
- rhs_quos <- lapply(rhs, function(x) new_quosure(x))[-1]
- dots_list <- map2(lhs_quos, rhs_quos, ~quos(!!quo_get_expr(.x) := !!.y))
- dots <- unlist(dots_list)
- mutate(.data, !!! dots)
- }
- mutate_double_sided(FB, .(adjusted2, clo) := .(adjusted+2, close))
- #> # A tibble: 1,008 x 10
- #> symbol date open high low close volume adjusted adjusted2
- #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
- #> 1 FB 2013-01-02 27.4 28.2 27.4 28.0 69846400. 28.0 30.0
- #> 2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600. 27.8 29.8
- #> 3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400. 28.8 30.8
- #> 4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800. 29.4 31.4
- #> 5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300. 29.1 31.1
- #> 6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700. 30.6 32.6
- #> 7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400. 31.3 33.3
- #> 8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000. 31.7 33.7
- #> 9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800. 31.0 33.0
- #> 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600. 30.1 32.1
- #> # ... with 998 more rows, and 1 more variable: clo <dbl>
- # ------------------------------------------------------------------------
- # A more practical example
- # Rolling correlations with the lhs being the x's and the rhs being corresponding y's
- rolling_cor_pairs <- function(.data, x, window = 5) {
- roll_cor <- rollify(~cor(.x, .y), window = window)
- dot_equal <- enquo(x)
- dot_expr <- quo_get_expr(dot_equal)
- dot_env <- quo_get_env(dot_equal)
- lhs <- dot_expr[[2]]
- rhs <- dot_expr[[3]]
- lhs_quos <- lapply(lhs, function(x) new_quosure(x))[-1]
- rhs_quos <- lapply(rhs, function(x) new_quosure(x))[-1]
- dots_list <- map2(lhs_quos, rhs_quos, ~quos(!! paste0(quo_name(.x), "_", quo_name(.y)) := roll_cor(!!quo_get_expr(.x), !!quo_get_expr(.y))))
- dots <- unlist(dots_list)
- mutate(.data, !!! dots)
- }
- rolling_cor_pairs(FB, .(adjusted, open, high) := .(open, high, adjusted), window = 5) %>%
- select(date, open, high, adjusted, adjusted_open, open_high, high_adjusted)
- #> # A tibble: 1,008 x 7
- #> date open high adjusted adjusted_open open_high high_adjusted
- #> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
- #> 1 2013-01-02 27.4 28.2 28.0 NA NA NA
- #> 2 2013-01-03 27.9 28.5 27.8 NA NA NA
- #> 3 2013-01-04 28.0 28.9 28.8 NA NA NA
- #> 4 2013-01-07 28.7 29.8 29.4 NA NA NA
- #> 5 2013-01-08 29.5 29.6 29.1 0.749 0.879 0.946
- #> 6 2013-01-09 29.7 30.6 30.6 0.805 0.882 0.980
- #> 7 2013-01-10 30.6 31.5 31.3 0.859 0.919 0.985
- #> 8 2013-01-11 31.3 32.0 31.7 0.884 0.927 0.990
- #> 9 2013-01-14 32.1 32.2 31.0 0.667 0.931 0.887
- #> 10 2013-01-15 30.6 31.7 30.1 0.379 0.948 0.316
- #> # ... with 998 more rows
Add Comment
Please, Sign In to add comment