0

I am using Quantmode to extract a list of stock prices from Yahoo (Close price only), then write them into excel. I would like to get rid of "NA" values (resulting from non-trading days in certain country), and replace it with the prior value (the price of the day before).

> library(quantmod)

> x <- getSymbols("X", src ="yahoo", from=Sys.Date()-365, to=Sys.Date(), auto.assign=FALSE )
> x.close <- x[,4]
> y <- getSymbols("Y", src ="yahoo", from=Sys.Date()-365, to=Sys.Date(), auto.assign=FALSE )
> y.close <- y[,4]
> z <- getSymbols("Z", src ="yahoo", from=Sys.Date()-365, to=Sys.Date(), auto.assign=FALSE )
> z.close <- z[,4]

>ticker <- cbind(x.close, y.close, z.close)

>write.csv(as.data.frame(ticker), file="test.csv")

This would extract a lovely set of Close prices for stock items to an excel, however because it comes with "NA" values, it would disturb my functions when formatting in excel.

Could it really be possible for me to set something within R that could replace the NA after pulling the set from Yahoo?

Thank you in advance,

Dan Lim
  • 15
  • 4
  • 2
    You can check the `na.locf` from `zoo` – akrun Jun 28 '19 at 08:22
  • Depending on what replacement you want for NA you could use `ticker[is.na(ticker)] <- replacement_value`. See this [SO question](https://stackoverflow.com/questions/8161836/how-do-i-replace-na-values-with-zeros-in-an-r-dataframe) for more examples. – cbo Jun 28 '19 at 10:12

0 Answers0