2

I am trying to calculate rolling correlation on a tibble, iterating through column names in a loop. I seem to be struggling passing variables to a function, though. This works:

tbl <- tibble(date = seq(as.Date("1983-03-31"), by=7, length.out=100), 
    col1 = 1:100, col2 = sample(100, size = 100, replace=TRUE), col3 = col1 + col2)
tbl %>% 
    tq_mutate_xy(
        x = col1,
        y = col3,
        mutate_fun = runCor,
        n = 10,
        use = "pairwise.complete.obs",
        col_rename = "col1_col3_corr"
      )

But this doesn't:

tbl <- tibble(date = seq(as.Date("1983-03-31"), by=7, length.out=100), 
    col1 = 1:100, col2 = sample(100, size = 100, replace=TRUE), col3 = col1 + col2)
c1 <- "col1"
c2 <- "col3"
tbl %>% 
    tq_mutate_xy(
        x = !!c1,
        y = !!c2,
        mutate_fun = runCor,
        n = 10,
        use = "pairwise.complete.obs",
        col_rename = paste0(c1, "_", c2, "_corr")
    )

The error is "Error in check_x_y_valid(data, x, y) : x = !(!c1) not a valid name."

What am I doing wrong?

rinspy
  • 386
  • 1
  • 10
  • You may want to tag this with `[tidyquant]` if that's where `tq_mutate_xy` is from – arvi1000 Apr 12 '18 at 15:24
  • You might have to use `!!rlang::sym(c1)` instead of `!!c1`. There are numerous examples on this site. – mt1022 Apr 12 '18 at 15:27
  • @mt1022 Error in check_x_y_valid(data, x, y) : x = !(!rlang::sym(c1)) not a valid name. – rinspy Apr 12 '18 at 15:37
  • @arvi1000 Done. Although if there are more straightforward ways of calculating rolling correlations, I am open to suggestions. – rinspy Apr 12 '18 at 15:38
  • you can do `!!rlang::as_quosure(as.name(col1))` instead of `!!col1` This should work – Onyambu Apr 12 '18 at 15:47
  • @Onyambu Error in check_x_y_valid(data, x, y) : x = !(!rlang::as_quosure(as.name(c1))) not a valid name. – rinspy Apr 12 '18 at 16:01

1 Answers1

3

First, I think you want the non-standard evaluation (NSE) version of tq_mutate_xy -- that is, tq_mutate_xy_. As a result, when you use the NSE of these functions, you need to use character strings -- this means your mutate_fun variable should also be a character string. The following should work:

c1 <- "col1"
c2 <- "col3"
tbl %>% 
  tq_mutate_xy_(
    x = c1,
    y = c2,
    mutate_fun = "runCor",
    n = 10,
    use = "pairwise.complete.obs",
    col_rename = paste0(c1, "_", c2, "_corr")
  )

Be sure to look at example 5 from the help documentation, ?tq_mutate_xy

JasonAizkalns
  • 20,243
  • 8
  • 57
  • 116
  • That seems to have worked, thanks. When I tried it before, I did not put runCor in quotes, as I thought I should be passing a function, not name of function. That said, the error message I got was most unhelpful: "Error in match(x, table, nomatch = 0L) : 'match' requires vector arguments". Is there no typechecking that can be done on passed function parameters in R to allow more helpful error messages in such cases? – rinspy Apr 12 '18 at 16:08
  • @rinspy The packages are all open sourced by the folks at [business-science.io](http://www.business-science.io/). You might get further with filing an issue on the appropriate GitHub repo, or better yet, submitting a pull request (PR). – JasonAizkalns Apr 12 '18 at 16:10