0

I have a pricedata object saved as an XTS with 315 columns and 3365 lines. There are price series of stock market securities.

> dim(pricedata)
[1] 3365  315

Problem is, sometimes (approx 10 times a year) it has NA values (market closed) and these are to be replaced by the previous value. Note that NA's can be 3-4 days in a row sometimes.

My code:

pricedata_carry = pricedata   
     for (x in 1:length(colnames(pricedata)))
       {
        list_na = which(is.na(pricedata[,x]))

            for (y in 1:(length(list_na)))
             {          
               pricedata_carry[list_na[y],x] =  pricedata_carry[list_na[y]-1,x]             
             }
        }

But this is very slow and I wonder if there is a faster way to do this.

Arkadi w
  • 129
  • 22
  • 3
    see this function:https://www.rdocumentation.org/packages/zoo/versions/1.8-4/topics/na.locf. Lets you replace with last non-NA value. – RLave Nov 30 '18 at 13:42
  • This is just excellent. pricedata_carry = na.locf(pricedata_carry) works just fine. Quick question: what do I do when my series start with NA? I would like to avoid the loop. – Arkadi w Nov 30 '18 at 13:51
  • See the function fill from tidyr, where you can decide the direction. Then you can ask if the first value is NA, if so you can use fill with option up for this case for all other cases. or something like `ifelse(is.na(fill(YourVar), fill(YourVar, direction = "up", fill(YourVar))` – floe Nov 30 '18 at 14:21

1 Answers1

0

there are two possibilties to do that:

first na.locf --> Last Observation carry forward from package zoo

or second fill from package tidyr where you can decide wich value last or next should be used.

floe
  • 417
  • 4
  • 12