Trim the NA's off the right end using na.trim
and then take the length of that to find the position (or use end
in place of length
if you want the time of the last non-NA value).
library(xts)
x <- xts(c(NA, 1, 2, 3, 4, NA), as.Date("2000-01-01")+0:5) # test input
length(na.trim(x, sides = "right"))
## [1] 5
To get the positions of both the start and end at the same time or just use rng
if you want the times instead.
rng <- range(time(na.trim(x)))
match(rng, time(x))
## [1] 2 5
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…