tidyquant icon indicating copy to clipboard operation
tidyquant copied to clipboard

tq_mutate_xy: Issue when column names are x and y

Open mdancho84 opened this issue 7 years ago • 1 comments

Error with _xy functions when data frame names are x and y.

    library(tidyquant)  # Loads tidyverse, tidyquant, financial pkgs, xts/zoo   
    df <- tibble(
        date = seq.Date(from = as.Date("2017-01-01"), by = 1, length.out = 100),
        x    = 1:100 + rnorm(100),
        y    = 1:100 + rnorm(100)
    )
    
    df %>%
        tq_mutate_xy(x, y, mutate_fun = runCor)
    #> Error in runCov(x, y, n, use = use, sample = sample, cumulative): n = 10 is outside valid range: [1, 1]
    
    df <- tibble(
        date = seq.Date(from = as.Date("2017-01-01"), by = 1, length.out = 100),
        a    = 1:100 + rnorm(100),
        b    = 1:100 + rnorm(100)
    )
    
    df %>%
        tq_mutate_xy(a, b, mutate_fun = runCor)
    #> # A tibble: 100 x 4
    #>          date        a         b     value
    #>        <date>    <dbl>     <dbl>     <dbl>
    #>  1 2017-01-01 1.526324  1.593958        NA
    #>  2 2017-01-02 0.760388  2.731137        NA
    #>  3 2017-01-03 3.835850  5.925257        NA
    #>  4 2017-01-04 4.069199  3.291095        NA
    #>  5 2017-01-05 5.554439  4.438022        NA
    #>  6 2017-01-06 4.992412  7.368362        NA
    #>  7 2017-01-07 5.979709  6.281690        NA
    #>  8 2017-01-08 6.811017  7.150365        NA
    #>  9 2017-01-09 8.883805  9.177332        NA
    #> 10 2017-01-10 9.684047 11.369196 0.9135464
    #> # ... with 90 more rows

mdancho84 avatar Oct 24 '17 16:10 mdancho84

I can see two ways of solving this.

One rename the x variable in the code to something else. Ideally after the check_x_y_valid check. Doing something like this works. And the return values from TTR are not There are more packages that handle weird column names, but I'm not sure if just halting the code would be better

.....
  # Check x and y 
  check_x_y_valid(data, x, y)
  # rename column called x to something else so TTR::runXXX functions work
  if (x == "x") {
    data <- dplyr::rename(data, renamed_x = x)
    x <- "renamed_x"
  }
  
# repeat for y

Or two, adjust the check_x_y_valid check and stop the code if the x variable is called x. Something like if (x == "x") stop(paste0("x is called ", x, " . This is not a valid column name. Please rename the column in your data before continuing"))

If you want me to create a PR with the preferred solution, let me know and I will create one.

pverspeelt avatar Aug 11 '18 15:08 pverspeelt