Reputation: 129
I am trying to get the past 4 fiscal years' stock price, which are the close price for each specific date from yahoo using R.
The following is:
getFin("AAPL")
viewFin(AAPL.f, "IS", "A")
x <- viewFin(AAPL.f, "IS", "A")
y <- colnames(x)
getSymbols("AAPL")
AAPL[y]
But I find that I can not get the price while I was using AAPL[y], it gave me nothing.
Can someone give me a hand? Thanks!!
Upvotes: 2
Views: 1215
Reputation: 176648
AAPL[y]
returned an empty xts object because AAPL didn't trade on those dates. You can get the previous close by merging AAPL
with an empty xts object containing the y
Dates and using na.locf
.
Note that if fill
is a function, the function will be applied to the result of the merge before merge
returns. It's a handy feature.
merge(AAPL, xts(,sort(as.Date(y))), fill=na.locf)[y,]
Upvotes: 2