hartmut
hartmut

Reputation: 982

which(is.na(xtsSeries)) returns out of bounds values

I have some time series in a xts object, with dimensions 54 * 5. I am interested in knowing which rows contain NAs. A call to which(is.na(.)) gives me values that are greater than 54 (namely: 85, 108, ..). I don't understand what these values are?

> typeof(dataXtsW)
[1] "double"
> class(dataXtsW)
[1] "xts" "zoo"
> dim(dataXtsW)
[1] 54  5
> which(is.na(dataXtsW))
[1]  54  85 108 162 216
> dataXtsW[85]
Error in `[.xts`(dataXtsW, 85) : subscript out of bounds
> dataXtsW[85,]
Error in `[.xts`(dataXtsW, 85, ) : subscript out of bounds
> dataXtsW[54,]
           NWHLNYHL Index LUHYTOBS Index SUM INX Index PCUSEQTR Index VIX Index
2017-04-21             NA             NA            NA             NA 0.1305778

Upvotes: 1

Views: 126

Answers (2)

Alex Deckmyn
Alex Deckmyn

Reputation: 1037

in your example, which() will treat the data as a vector of length 54*5. Data in a matrix is stored column by column, so element 54 is the last element of the first column, 108 is the last element of the second column etc.

if you use which( , arr.ind = TRUE) you will get the array index (row,column) of the NA elements.

Upvotes: 2

ikop
ikop

Reputation: 1790

If you want to know which rows contain NA you can find out using e.g.

which(apply(dataXtsW, MARGIN = 1, FUN = function(x) any(is.na(x))))

where you check each row (MARGIN = 1) for NA values (any(is.na(x)))). If you use which(is.na(dataXtsW)), it returns the indixes of the NA elements of your data frame (not the rows!). The indices refer to the elements, ordered column-wise. In your case, the 54th element is NA. You can check this using

 unlist(dataXtsW)[54]

Upvotes: 1

Related Questions