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
I have a Data frame like this I want to write a efficient code to fill up the NA with previous Quote bid and ask, Time is sorted, and only Quote contains bid and ask field (preferably vectorization)
so it becomes
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
thanks
Use the fillna() Method: The fillna() function iterates through your dataset and fills all null rows with a specified value. It accepts some optional arguments—take note of the following ones: Value: This is the value you want to insert into the missing rows. Method: Lets you fill missing values forward or in reverse.
To fill in the missing values, we can highlight the range starting before and after the missing values, then click Home > Editing > Fill > Series. What is this? If we select the Type as Growth and click the box next to Trend, Excel automatically identifies the growth trend in the data and fills in the missing values.
Using mean values for replacing missing values may not create a great model and hence gets ruled out. For symmetric data distribution, one can use the mean value for imputing missing values.
Replace_NA. Function replaces missing values in selected columns using defined value.
The na.locf()
function in the zoo package is your friend here. The locf
stands for "last one carried forward". With your data:
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))
Produces.
> 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
Try this:
# 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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With