“非数值参数到二进制操作符”错误来自getreturn

时间:2021-07-15 12:36:55

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.

由于某些原因,我通常在rstudio中运行的代码不再工作。我希望有人也有过类似的经历,并理解发生了什么。

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 * that addresses this issue. Also, I saw that the most recent documentation, from July 2014 doesn't mention anything either:

我在网上找不到任何解决这个问题的方法,也找不到*。此外,我还看到,2014年7月的最新文件也没有提及任何内容:

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

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

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

有人知道这里发生了什么吗?

2 个解决方案

#1


5  

It's probably a function name * 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

您一定已经加载了stockPortfolio包,然后加载了timeSeries或另一个依赖于timeSeries的包。查看一下您的控制台,看看是否有类似的消息

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.

使用双冒号操作符(如上所示)显式地告诉R要查看的包。

#2


0  

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 - Markdown程序中使用stockPortfolio。在R文件中工作的代码在rmd文件中不工作。

    ```{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:

我的recordState函数将search()和sessionInfo()的结果保存在块中:

     [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.

最初的帖子表明,这个错误可能是由于在timeSeries中混淆了stockPortfolio:: getreturn和函数,但是我使用了全名,并且没有加载任何一个库。

#1


5  

It's probably a function name * 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

您一定已经加载了stockPortfolio包,然后加载了timeSeries或另一个依赖于timeSeries的包。查看一下您的控制台,看看是否有类似的消息

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.

使用双冒号操作符(如上所示)显式地告诉R要查看的包。

#2


0  

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 - Markdown程序中使用stockPortfolio。在R文件中工作的代码在rmd文件中不工作。

    ```{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:

我的recordState函数将search()和sessionInfo()的结果保存在块中:

     [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.

最初的帖子表明,这个错误可能是由于在timeSeries中混淆了stockPortfolio:: getreturn和函数,但是我使用了全名,并且没有加载任何一个库。