1) na.locf
works on zoo objects, vectors and matrices, not data.frames, so try this which applies it separately to each column and then reconstructs the data.frame:
ddply(DF, .(fx_code), function(x) replace(x, TRUE, lapply(x, na.locf))
2) or this:
na.locf.data.frame <-
function(object, ...) replace(object, TRUE, lapply(object, na.locf, ...))
ddply(DF, .(a), na.locf)
3) If the dates of DF
are unique within fx_code
then it can be represented as a wide zoo object but not as a long one as zoo objects are based on matrices or vectors so their columns must all be of the same class. For the data frame shown this would work if the dates are unique within fx_code
:
z <- read.zoo(DF, split = 1, index = 2)
na.locf(z)
For example, try it with this:
DF <- data.frame(a = c("a", "a", "b", "b"), b = Sys.Date() + 0:3, c = 1:4)
Note
Since this was written na.locf
now works on data frames as well.