joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
220 stars 71 forks source link

diff.xts on boolean xts drops colnames #315

Closed sdittmar closed 4 years ago

sdittmar commented 4 years ago

Description

When called on an xts object that contains logical values, diff.xts will not carry on the colnames of the xts object.

Expected behavior

The issue seems to be at line 102 of lag.xts.R since "as.integer(x)" will drop the colnames.

I would fix it like this:

tmp <- colnames(x) x <- .xts(matrix(as.integer(x),ncol=NCOL(x)), .index(x), tclass(x)) colnames(x) <- tmp

But then I am not a R-wizard... ;-)

Btw, thank you for all these great R-packages!

Minimal, reproducible example

x <- xts(matrix(rnorm(100), ncol = 10), Sys.Date() + 1:10)
colnames(x) <- letters[1:10]
x <- x > 0
str(x)
y <- diff(x)
str(y)

Session Info

R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 9 (stretch)

Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.7.0
LAPACK: /usr/lib/lapack/liblapack.so.3.7.0

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

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

other attached packages:
[1] xts_0.11-2 zoo_1.8-4 

loaded via a namespace (and not attached):
[1] compiler_3.5.2  tools_3.5.2     grid_3.5.2      lattice_0.20-38
joshuaulrich commented 4 years ago

Thanks for the report and suggested patch! I can replicate this with the latest development version.