0

Language: R Data: from "Yahoo! Finance"

I am trying to delete 100 rows of data from a stock price dataset and then I want to plot the "Close" column. I expect to see a straight line inside the graph(for the missing values area). When I am running the following code, I get the below error message.

CODE:

library(quantmod)
alt.data.AMZN <- getSymbols("AMZN", from="2010-12-31", to="2013-12-31", auto.assign = FALSE)
class(alt.data.AMZN)
head(alt.data.AMZN)
plot(alt.data.AMZN$AMZN.Close)
#Deleting a few rows of data from the xts object.
data.missing <- alt.data.AMZN[c(-400:-500),]
head(data.missing)
plot(data.missing$AMZN.close) #This is where I get the error

Error message**: Error in plot.window(...) : need finite 'xlim' values In addition: Warning messages: 1: In min(x) : no non-missing arguments to min; returning Inf 2: In max(x) : no non-missing arguments to max; returning -Inf 3: In min(x) : no non-missing arguments to min; returning Inf 4: In max(x) : no non-missing arguments to max; returning -Inf

  • 2
    You have a typo in there. Try `plot(data.missing$AMZN.Close)`. Also why you use `alt.data.AMZN` when you have stored data in `data.AMZN` ? – Ronak Shah Dec 14 '20 at 07:47
  • Hi Ronak, sorry for the typo. I checked my code and I had stored it as alt.data.AMZN only. I had deleted the "alt" by mistake while posting the question and while formatting it on stackoverflow. – Anoop Bose Dec 17 '20 at 17:05

1 Answers1

0

To delete the rows you could set them equal to NA. Then the plot won't show any values for the missing values. If you want a straight line, you can run na.locf on the resulting xts and you'll end up with a straight line for the missing values.

data.missing <- alt.data.AMZN
data.missing[c(400:500), ] <- NA
plot(data.missing$AMZN.Close)

plot with missing values

data.missing2 <- na.locf(data.missing)
plot(data.missing2$AMZN.Close)

plot with a straight line

tester
  • 1,662
  • 1
  • 10
  • 16