"non-numeric argument to binary operator" error from getReturns

2k views Asked by At

For some reason, a code I usually run in Rstudios is no longer working. I'm hoping that someone has had a similar experience and understands what's going on.

getReturns(c('C','BAC'), start='2004-01-01', end='2008-12-31')

This results in:

Error in unclass(e1) + unclass(e2) : 
non-numeric argument to binary operator

I can't find anything online nor on stackoverflow that addresses this issue. Also, I saw that the most recent documentation, from July 2014 doesn't mention anything either:

http://cran.r-project.org/web/packages/stockPortfolio/stockPortfolio.pdf

Does anyone have any idea what's going on here?

2

There are 2 answers

0
Richie Cotton On BEST ANSWER

It's probably a function name clash issue. Running

timeSeries::getReturns(c('C','BAC'), start='2004-01-01', end='2008-12-31')

gives me the error, but running

stockPortfolio::getReturns(c('C','BAC'), start='2004-01-01', end='2008-12-31')

works fine.


How did this happen?

You must have loaded the stockPortfolio package, and then loaded either timeSeries or another package that depends upon timeSeries. Have a look through your console for a message that looks like

The following object is masked from ‘package:stockPortfolio’:

    getReturns

Use the double colon operator (as shown above) to explicitly tell R which package to look in.

0
Nguinasso On

I have a similar problem using stockPortfolio in a R Markdown program. Code that works in a R file does not work in the rmd file.

    ```{r p3}
    recordState()
    ff <- allFunds1$Fund
    returns  <-stockPortfolio::getReturns(ff,freq="month")
    save(allFunds1,file='allFunds1.rda')
    ```

gives the error message and traceback

    Error in unclass(e1) + unclass(e2) : non-numeric argument to    binary operator
    5. structure(unclass(e1) + unclass(e2), class = "Date")
    4.`+.Date`(as.Date(origin, ...), x)
    3. as.Date.numeric(uDates, origin = minDate)
    2. as.Date(uDates, origin = minDate
    1. stockPortfolio::getReturns(ff, freq = "month")

My recordState function saves the results of search() and sessionInfo() in the chunk:

     [1] "search:"
     [1] ".GlobalEnv"        "tools:rstudio"     "package:stats"    
     [4] "package:graphics"  "package:grDevices" "package:utils"    
     [7] "package:datasets"  "package:methods"   "Autoloads"        
     [10] "package:base"     
     [1] "sessionInfo():"
     R version 3.3.2 (2016-10-31)
     Platform: x86_64-apple-darwin13.4.0 (64-bit)
    Running under: OS X Yosemite 10.10.5

    locale:
    [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

    attached base packages:
    [1] stats     graphics  grDevices utils     datasets  methods      base     

    loaded via a namespace (and not attached):
    [1] Rcpp_0.12.9        digest_0.6.11      dplyr_0.5.0       
    [4] rprojroot_1.2      assertthat_0.1     R6_2.2.0          
    [7] xtable_1.8-2       DBI_0.5-1          backports_1.0.5   
   [10] magrittr_1.5       evaluate_0.10      stringi_1.1.2     
   [13] stockPortfolio_1.2 rmarkdown_1.3      tools_3.3.2       
   [16] stringr_1.1.0      readr_1.0.0        yaml_2.1.14       
   [19] htmltools_0.3.5    knitr_1.15.1       tibble_1.2        

The original posting suggests that this error can result from confusing stockPortfolio::getReturns with the function in timeSeries but I have used the full name and do not have either of the libraries loaded.