I have this time series as
Quant1 Quant2
2013-01-23 400 200
2013-01-22 0 0
2013-01-21 0 0
2013-01-20 125 100
2013-01-18 120 0
And wants output as
Quant1 Quant2
2013-01-23 400 200
2013-01-22 125 100
2013-01-21 125 100
2013-01-20 125 100
2013-01-18 120 0
I am trying this, but it does not seem to work. I am getting null error NULL Warning encountered while processing method
replace(df,df == 0, NA)
df <- na.locf(df)
df[is.na(df)] <- 0
Any suggestions?
Update
As per most voted answer I tried (I modified input dates)
> z <- structure(c(400L, 0L, 0L, 125L, 120L, 200L, 0L, 0L, 100L,
+ 0L), .Dim = c(5L, 2L), .Dimnames = list(NULL, c("Quant1", "Quant2"
+ )), index = structure(c(15728, 15727, 15726, 15725, 15723), class = "Date"),
+ class = "zoo")
> z
Quant1 Quant2
2013-01-23 400 200
2013-01-22 0 0
2013-01-21 0 0
2013-01-20 125 100
2013-01-18 120 0
> L <- rowSums(z != 0) > 0
> z[] <- coredata(z)[which(L)[cumsum(L)],]
> z
Quant1 Quant2
2013-01-23 400 200
2013-01-22 0 0
2013-01-21 0 0
2013-01-20 0 0
2013-01-18 120 0