Reputation: 837
I have basic xts object generated using the following.
library(quantmod)
temp1 <- getSymbols("GOOG",src = 'yahoo',from=Sys.Date()-50,to = Sys.Date(),auto.assign=FALSE)
temp2 <- temp1$GOOG.Close
head(temp2)
GOOG.Close
2020-05-20 1406.72
2020-05-21 1402.80
2020-05-22 1410.42
2020-05-26 1417.02
2020-05-27 1417.84
2020-05-28 1416.73
lag(temp2,1)
Error in c.xts(NA_real_, c(1406.719971, 1402.800049, 1410.420044, 1417.02002, :
zero-length vectors with non-zero-length index are not allowed
As far as I can tell temp2 is not zero length so I don't understand the error.
Similarly the diff(temp2) function does work as expected.
This seems to have started when I recently updated to R 4.0.1
I cannot find any explanation for this error considering I do have a non-zero vector.
Upvotes: 2
Views: 2483
Reputation: 176648
Do you have dplyr
loaded, by chance? That's the only way I can replicate your error.
dplyr
masks the stats::lag()
generic function that ships with base R. That breaks method dispatch for all S3 classes that define a lag()
method. So that means lag()
may or may not work for those classes.
R> head(lag(temp2, 1))
GOOG.Close
2020-05-26 NA
2020-05-27 1417.02
2020-05-28 1417.84
2020-05-29 1416.73
2020-06-01 1428.92
2020-06-02 1431.82
R> suppressPackageStartupMessages(library(dplyr))
R> head(lag(temp2, 1))
Error in c.xts(NA_real_, c(1417.02002, 1417.839966, 1416.72998, 1428.920044, :
zero-length vectors with non-zero-length index are not allowed
So you either need to: 1) not load dplyr, or 2) explicitly call stats::lag()
everywhere you don't want to use dplyr::lag()
.
Upvotes: 6