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.