Guest User

Untitled

a guest
Mar 18th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. library(rlang)
  2. library(dplyr)
  3. library(tibbletime)
  4. library(purrr)
  5. data(FB)
  6.  
  7. # ------------------------------------------------------------------------
  8. # Call mutate, but with the lhs being names and the rhs being expressions
  9.  
  10. mutate_double_sided <- function(.data, x) {
  11.  
  12. dot_equal <- enquo(x)
  13. dot_expr <- quo_get_expr(dot_equal)
  14. dot_env <- quo_get_env(dot_equal)
  15. lhs <- dot_expr[[2]]
  16. rhs <- dot_expr[[3]]
  17.  
  18. lhs_quos <- lapply(lhs, function(x) new_quosure(x))[-1]
  19. rhs_quos <- lapply(rhs, function(x) new_quosure(x))[-1]
  20.  
  21. dots_list <- map2(lhs_quos, rhs_quos, ~quos(!!quo_get_expr(.x) := !!.y))
  22.  
  23. dots <- unlist(dots_list)
  24.  
  25. mutate(.data, !!! dots)
  26. }
  27.  
  28. mutate_double_sided(FB, .(adjusted2, clo) := .(adjusted+2, close))
  29. #> # A tibble: 1,008 x 10
  30. #> symbol date open high low close volume adjusted adjusted2
  31. #> <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  32. #> 1 FB 2013-01-02 27.4 28.2 27.4 28.0 69846400. 28.0 30.0
  33. #> 2 FB 2013-01-03 27.9 28.5 27.6 27.8 63140600. 27.8 29.8
  34. #> 3 FB 2013-01-04 28.0 28.9 27.8 28.8 72715400. 28.8 30.8
  35. #> 4 FB 2013-01-07 28.7 29.8 28.6 29.4 83781800. 29.4 31.4
  36. #> 5 FB 2013-01-08 29.5 29.6 28.9 29.1 45871300. 29.1 31.1
  37. #> 6 FB 2013-01-09 29.7 30.6 29.5 30.6 104787700. 30.6 32.6
  38. #> 7 FB 2013-01-10 30.6 31.5 30.3 31.3 95316400. 31.3 33.3
  39. #> 8 FB 2013-01-11 31.3 32.0 31.1 31.7 89598000. 31.7 33.7
  40. #> 9 FB 2013-01-14 32.1 32.2 30.6 31.0 98892800. 31.0 33.0
  41. #> 10 FB 2013-01-15 30.6 31.7 29.9 30.1 173242600. 30.1 32.1
  42. #> # ... with 998 more rows, and 1 more variable: clo <dbl>
  43.  
  44. # ------------------------------------------------------------------------
  45. # A more practical example
  46. # Rolling correlations with the lhs being the x's and the rhs being corresponding y's
  47.  
  48. rolling_cor_pairs <- function(.data, x, window = 5) {
  49.  
  50. roll_cor <- rollify(~cor(.x, .y), window = window)
  51.  
  52. dot_equal <- enquo(x)
  53. dot_expr <- quo_get_expr(dot_equal)
  54. dot_env <- quo_get_env(dot_equal)
  55. lhs <- dot_expr[[2]]
  56. rhs <- dot_expr[[3]]
  57.  
  58. lhs_quos <- lapply(lhs, function(x) new_quosure(x))[-1]
  59. rhs_quos <- lapply(rhs, function(x) new_quosure(x))[-1]
  60.  
  61. 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))))
  62.  
  63. dots <- unlist(dots_list)
  64.  
  65. mutate(.data, !!! dots)
  66. }
  67.  
  68. rolling_cor_pairs(FB, .(adjusted, open, high) := .(open, high, adjusted), window = 5) %>%
  69. select(date, open, high, adjusted, adjusted_open, open_high, high_adjusted)
  70. #> # A tibble: 1,008 x 7
  71. #> date open high adjusted adjusted_open open_high high_adjusted
  72. #> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
  73. #> 1 2013-01-02 27.4 28.2 28.0 NA NA NA
  74. #> 2 2013-01-03 27.9 28.5 27.8 NA NA NA
  75. #> 3 2013-01-04 28.0 28.9 28.8 NA NA NA
  76. #> 4 2013-01-07 28.7 29.8 29.4 NA NA NA
  77. #> 5 2013-01-08 29.5 29.6 29.1 0.749 0.879 0.946
  78. #> 6 2013-01-09 29.7 30.6 30.6 0.805 0.882 0.980
  79. #> 7 2013-01-10 30.6 31.5 31.3 0.859 0.919 0.985
  80. #> 8 2013-01-11 31.3 32.0 31.7 0.884 0.927 0.990
  81. #> 9 2013-01-14 32.1 32.2 31.0 0.667 0.931 0.887
  82. #> 10 2013-01-15 30.6 31.7 30.1 0.379 0.948 0.316
  83. #> # ... with 998 more rows
Add Comment
Please, Sign In to add comment