[R] LOCF - Last Observation Carried Forward

Tony Plate tplate at acm.org
Fri Nov 14 18:20:17 CET 2003


Here's a function that does the essential computation (written to work in 
both S-plus and R).

This looks like one of those tricky problems that do not vectorize 
easily.  It would be simple to write a C-program to compute this very 
efficiently.  But are there any more efficient solutions than ones like the 
below (that are written without resort to C)?

most.recent <- function(x) {
     # return a vector of indices of the most recent TRUE value
     if (!is.logical(x))
         stop("x must be logical")
     x[is.na(x)] <- FALSE
     # x is a logical vector
     r <- rle(x)
     ends <- cumsum(r$lengths)
     starts <- ends - r$lengths + 1
     spec <- as.list(as.data.frame(rbind(start=starts, len=r$lengths, 
value=as.numeric(r$values), prev.end=c(NA, ends[-length(ends)]))))
     names(spec) <- NULL
     unlist(lapply(spec, function(s) if (s[3]) seq(s[1], len=s[2]) else 
rep(s[4], len=s[2])), use.names=F)
}

 > x <- c(F,T,T,F,F,F,T,F)
 > most.recent(x)
[1] NA  2  3  3  3  3  7  7

And using it to do the fill-forward:

 > x <- c(NA,2,3,NA,4,NA,5,NA,NA,NA,6,7,8,NA)
 > x[most.recent(!is.na(x))]
  [1] NA  2  3  3  4  4  5  5  5  5  6  7  8  8
 >

Some timings:

 > x <- sample(c(T,F),1e4,rep=T)
 > system.time(most.recent(x))
[1] 0.33 0.01 0.47   NA   NA
 > x <- sample(c(T,F),1e5,rep=T)
 > system.time(most.recent(x))
[1] 4.27 0.06 6.44   NA   NA
 > x <- sample(c(T,F),1e6,rep=T)
 > system.time(most.recent(x))
[1] 47.27  0.17 47.97    NA    NA
 >

-- Tony Plate

PS. Actually, I just found a solution that I had lying around that is about 
70 times as fast on random test data like the above.


At Friday 03:07 PM 11/14/2003 +0100, Karl Knoblick wrote:
>Hi!
>
>Is there a possibilty in R to carry out LOCF (Last Observation Carried 
>Forward) analysis or to create a new data frame (array, matrix) with LOCF? 
>Or some helpful functions, packages?
>
>Karl
>
>
>
>---------------------------------
>Gesendet von http://mail.yahoo.de
>Schneller als Mail - der neue Yahoo! Messenger.
>         [[alternative HTML version deleted]]
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Tony Plate   tplate at acm.org




More information about the R-help mailing list