[R-sig-finance] How can I do this better? (Filling in last traded price for NA)

Gabor Grothendieck ggrothendieck at myway.com
Tue Sep 14 03:50:51 CEST 2004


Dirk,

I was not aware that locf was in "its" but was aware of Tony's
solution as we had discussed both it and a forerunner of the solution
in my last post at that time.  See the thread beginning with:

https://stat.ethz.ch/pipermail/r-help/2003-November/040603.html

The two solutions are the same except for the inner portion
which calculates the indices of the LOCF of a logical 
vector.  Simplifying slightly:

most.recent.1 <- function(L) {
	if (length(L) > 1) L[1] <- TRUE
	w <- which(c(L,T))
	rep(w[-length(w)], diff(w))
}

most.recent.2 <- function(L) {
	which(c(NA,L))[cumsum(L)+1]
}

so the key operations are which, rep and diff in #1 and which, [
and cumsum in #2.  This suggests they are about equal in speed
and, in fact, some timings I did fluctuated from run to run but
in general they seemed to run at about the same speed with #1
running faster sometimes and #2 running faster other times
(even though the same input was used on every run).



More information about the R-sig-finance mailing list