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

subset xts by logical matrix throws an error #331

Open
cloudcell opened this issue Apr 5, 2020 · 9 comments
Open

subset xts by logical matrix throws an error #331

cloudcell opened this issue Apr 5, 2020 · 9 comments
Labels

Comments

@cloudcell
Copy link
Contributor

cloudcell commented Apr 5, 2020

Description

I get an error from the following code:

xtsTS <- xts(x=1:3, order.by=Sys.Date()+1:3)
xtsTS <- cbind(xtsTS,X=c(NA,NA,NA))
xtsTS[is.na(xtsTS)]

Error in [.xts(xtsTS, is.na(xtsTS), ) : 'i' or 'j' out of range

Is it a bug in the xts package?

Expected behavior

the code is supposed to list all the values of the xts object == NA
It would also be great if it could work for is.finite() and is.infinite() and is.nan()
just as an example, this works for data.frames:

df <- data.frame(bb=c(1,2,NA), cc=c(1,NA,4))
df[is.na(df)]
# this code works, probably, it would be great if xts behaved the same way

Minimal, reproducible example

library(xts)
xtsTS <- xts(x=1:3, order.by=Sys.Date()+1:3)
xtsTS <- cbind(xtsTS,X=c(NA,NA,NA))
xtsTS[is.na(xtsTS),] # either this line
xtsTS[is.na(xtsTS)] # or this line produce almost identical error messages

Session Info

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Linux Mint 19.3

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1

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

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

other attached packages:
[1] xts_0.12-0 zoo_1.8-7 

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

Thanks for the report! I would say this is a bug, because it works with zoo objects:

library(xts)
z <- cbind(Z = zoo(1:3, Sys.Date()+1:3), X = c(NA,NA,NA))
z[is.na(z),]  # returns empty zoo object

So this should behave similarly to zoo objects instead of throwing an error.

@joshuaulrich
Copy link
Owner

Also, this isn't related to is.na(). It happens any time you subset by a logical matrix, so I'm going to update the issue title.

set.seed(21)
x <- .xts(matrix(rnorm(6),3), 1:3)
m <- coredata(x) < 0
x[m]          # error
as.zoo(x)[m]  # empty zoo object

@joshuaulrich joshuaulrich changed the title is.na() produces an error whereas it should list all the records in an xts object containing NAs subset xts by logical matrix throws an error Apr 5, 2020
@joshuaulrich
Copy link
Owner

It would also help to know your use case. I'm not sure zoo does what you expect either, but at least it doesn't error. We may need to create a feature request for the behavior you want.

@cloudcell
Copy link
Contributor Author

Well, I made a workaround for my case already. I am creating various financial indicators, some of them occasionally produce values that are either infinite or not a number (NaN can be considered as NA). So I had to develop a way to treat those exceptions at the time they are calculated rather than at the time they are already sitting in the xts table. However, the expression
z[is.na(z),] feels right. This is how it's supposed to work. Why would xts treat this subsetting any differently from how data frames do that?

@joshuaulrich
Copy link
Owner

joshuaulrich commented Aug 8, 2020

This is related to #63 and #198. It does not have a obvious solution... :-/

@MaxineTheCat
Copy link

Also, this isn't related to is.na(). It happens any time you subset by a logical matrix, so I'm going to update the issue title.

set.seed(21)
x <- .xts(matrix(rnorm(6),3), 1:3)
m <- coredata(x) < 0
x[m]          # error
as.zoo(x)[m]  # empty zoo object

This no longer works w/ zoo btw (R 4.0.4 and zoo package release 1.8-8)

@joshuaulrich
Copy link
Owner

This no longer works w/ zoo btw (R 4.0.4 and zoo package release 1.8-8)

Thanks for pointing that out @MaxineTheCat. @ggrothendieck and @zeileis, was this an intended change in zoo?

@ggrothendieck
Copy link

ggrothendieck commented Mar 5, 2021 via email

@zeileis
Copy link
Collaborator

zeileis commented Mar 6, 2021 via email

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

5 participants