Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace missing value with previous value [duplicate]

Tags:

r

zoo

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

like image 956
user2033963 Avatar asked Feb 01 '13 21:02

user2033963


People also ask

How do you replace missing values in a data set?

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.

How do I find and replace missing data in Excel?

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.

Should you replace missing values with mean?

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.

Which function is used to replace missing values?

Replace_NA. Function replaces missing values in selected columns using defined value.


2 Answers

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
like image 102
Gavin Simpson Avatar answered Sep 22 '22 23:09

Gavin Simpson


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
}
like image 42
Eldar Agalarov Avatar answered Sep 22 '22 23:09

Eldar Agalarov