与前值更换零我有这个时间序列为及时R系列
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
,并希望为
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
我想这个输出,但它似乎并没有工作。我越来越null error NULL Warning encountered while processing method
replace(df,df == 0, NA)
df <- na.locf(df)
df[is.na(df)] <- 0
有什么建议吗?
更新
按照大多数投票的答案我想(我修改输入日期)
> 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
是'df'数据框还是'xts'对象?将'dput(df)'的输出编辑到你的问题中。另外,添加所有相关的'library'调用。您应该阅读[本FAQ](http://stackoverflow.com/a/5963610/1412059)。 – Roland
这是时间序列数据。我已经使用read.zoo(mydataframe,header = TRUE,format =“%Y-%m-%d”,sep =“\ t”)转换了数据帧。 –
您是否阅读过'?replace'? – Henrik