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

Subsetting using [.xts drops classes when zero-width #359

Closed shikokuchuo closed 1 year ago

shikokuchuo commented 2 years ago

Description

Subsetting using [.xts drops classes when zero-width.

Think the culprit is the use of .xts() in if (length(j) == 0 || ... This is another inconsistency for zero-width objects like #355 although unrelated.

Expected behavior

All classes to be retained consistent with other subsetting.

Minimal, reproducible example

> object <- as.xts(AirPassengers)
> class(object) <- c("custom", "xts", "zoo")
> class(object)
[1] "custom" "xts"    "zoo"   
> class(object[,0])
[1] "xts" "zoo"

Session Info

R version 4.1.1 (2021-08-10)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04.3 LTS

Matrix products: default
BLAS/LAPACK: /opt/intel/oneapi/mkl/2021.4.0/lib/intel64/libmkl_rt.so.1

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

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

other attached packages:
[1] xts_0.12.1 zoo_1.8-9 

loaded via a namespace (and not attached):
[1] compiler_4.1.1  tools_4.1.1     grid_4.1.1      lattice_0.20-45