9
Event,Time,Bid,Offer
Quote,0.458338,9.77,9.78
Order,0.458338,NA,NA
Order,0.458338,NA,NA
Order,0.458338,NA,NA
Quote,0.458363,9.78,9.79
Order,0.458364,NA,NA

我有一个像这样的数据框我想编写一个有效的代码来用以前的报价报价和报价填充 NA,时间是排序的,只有报价包含报价和报价字段(最好是矢量化)

所以它变成了

Event,Time,Bid,Offer
Quote,0.458338,9.77,9.78
Order,0.458338,9.77,9.78
Order,0.458338,9.77,9.78
Order,0.458338,9.77,9.78
Quote,0.458363,9.78,9.79
Order,0.458364,9.78,9.79

谢谢

4

2 回答 2

21

zoo包中的na.locf()功能是你的朋友。代表“最后一个结转”。使用您的数据:locf

dat <- read.table(text = "Event,Time,Bid,Offer
Quote,0.458338,9.77,9.78
Order,0.458338,NA,NA
Order,0.458338,NA,NA
Order,0.458338,NA,NA
Quote,0.458363,9.78,9.79
Order,0.458364,NA,NA
", header = TRUE, sep = ",")

require(zoo)

dat2 <- transform(dat, Bid = na.locf(Bid), Offer = na.locf(Offer))

产生。

> dat2
  Event     Time  Bid Offer
1 Quote 0.458338 9.77  9.78
2 Order 0.458338 9.77  9.78
3 Order 0.458338 9.77  9.78
4 Order 0.458338 9.77  9.78
5 Quote 0.458363 9.78  9.79
6 Order 0.458364 9.78  9.79
于 2013-02-01T21:32:24.343 回答
2

尝试这个:

# Last Observation Move Forward
na.lomf <- function(object, na.rm = F) {
    na.lomf.0 <- function(object) {
        idx <- which(!is.na(object))
        if (is.na(object[1])) idx <- c(1, idx)
        rep.int(object[idx], diff(c(idx, length(object) + 1)))
    }    
    dimLen <- length(dim(object))
    object <- if (dimLen == 0) na.lomf.0(object) else apply(object, dimLen, na.lomf.0)
    if (na.rm) na.trim(object, sides = "left", is.na = "all") else object
}
于 2014-05-11T12:29:43.833 回答