Reputation: 2652
When calculating the returns of a time series of stock prices, it returns Inf
values for some dates. The series has a zooreg class and I am trying to replace those Inf
with NAs
.
For reproducibility, suppose I have the following object:
a <- structure(c(1, 2, 3, 2, 4, Inf, Inf, 3, 4), .Dim = c(3L, 3L), .Dimnames = list(NULL,
c("a", "b", "c")), index = structure(c(5113, 5144, 5173), class = "Date"),
frequency = 1, class = c("zooreg", "zoo"))
I tried the following
a[[a=Inf]]<-NA
but it gave an error:
Error in a[[a = Inf]] <- NA : attempt to select more than one element
How do I coerce the Inf
to NA
in this case?
Upvotes: 1
Views: 76
Reputation: 269860
Try this:
a[] <- replace(coredata(a), !is.finite(a), NA)
giving:
> a
a b c
1984-01-01 1 2 NA
1984-02-01 2 4 3
1984-03-01 3 NA 4
Upvotes: 1