Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as.zoo(dataframe, order.by=dates) breaks after loading quantmod #168

Closed
vtdmhd opened this issue Jun 16, 2017 · 2 comments
Closed

as.zoo(dataframe, order.by=dates) breaks after loading quantmod #168

vtdmhd opened this issue Jun 16, 2017 · 2 comments
Assignees
Labels

Comments

@vtdmhd
Copy link

vtdmhd commented Jun 16, 2017

Description

The function call from the tile stops working after loading quantmod, see MWE

Expected behavior

No interference with as.zoo()

Minimal, reproducible example

> vals <- data.frame(s=c(1,2,3))
> ds <- as.Date(c('2015-01-01','2016-01-01','2017-01-01'))
> as.zoo(vals, order.by=ds)            
           s                           
2015-01-01 1                           
2016-01-01 2                           
2017-01-01 3  
> library(quantmod)                    
Loading required package: TTR          
Version 0.4-0 included new data defaults. See ?getSymbols.                    
Learn from a quantmod author: https://www.datacamp.com/courses/importing-and-managing-financial-data-in-r                                                    
> as.zoo(vals, order.by=ds)            
Error in charToDate(x) :               
  character string is not in a standard unambiguous format

Session Info

> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Arch Linux

Matrix products: default
BLAS: /usr/lib/libblas_haswellp-r0.2.19.so
LAPACK: /usr/lib/liblapack.so.3.7.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] quantmod_0.4-9 TTR_0.23-1     xts_0.9-7      zoo_1.8-0     

loaded via a namespace (and not attached):
[1] compiler_3.4.0  tools_3.4.0     grid_3.4.0      lattice_0.20-35
@joshuaulrich
Copy link
Owner

Thanks for the report! I can replicate this, and will investigate.

@joshuaulrich joshuaulrich self-assigned this Jun 16, 2017
@joshuaulrich
Copy link
Owner

Well, this is a history lesson. quantmod:::as.zoo.data.frame was added in c2f9916, on 2007-07-01. The zoo package did not have an as.zoo.data.frame method until 2010-08-03.

The error is because the quantmod method has a row.date argument and also assumes that the data.frame rownames should always be used for the order.by argument. I think the solution is to simply call the zoo constructor if the user provides an order.by argument to the as.zoo call.

> git diff R/quantmod.R
diff --git a/R/quantmod.R b/R/quantmod.R
index 93dbc7f..11c21ca 100644
--- a/R/quantmod.R
+++ b/R/quantmod.R
@@ -12,8 +12,12 @@ function(x)
 `as.zoo.data.frame`<-
 function(x,row.date=TRUE,...)
 {
+    # ignore row.date if order.by is specified
+    if(hasArg(order.by)) {
+        zoo(x,...)
+    }
     #really need to test order - ???how?
-    if(row.date) {
+    else if(row.date) {
         zoo(x,as.Date(rownames(x),origin='1970-01-01'),...)
     }
     else {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants