1
votes

I am extracting a list of price of a stock (Open, High, Low, Close, Volume etc), from r (quantmod was used to bring in the data from external source) to excel, however the date column is omitted.

I thought the issue is in the fact that because the 'date' is not recognized as a variable, whereas other columns are (Open, High etc). I've tried to insert the dates via as.Date (from Sys.Date-365 to Sys.Date, as I only require a one year set of data), and cbind them. However because the length is different, and this is a vector based list, it won't fit (the data that comes from external source only shows in a working days, where as.Date will have full 365 days shown.

x <- getSymbols("SPY", src ="yahoo", from=Sys.Date()-365, to=Sys.Date(), auto.assign=FALSE )

write.csv(s, file = "test.csv")

Writing csv like above will result in the date column showing as 1, 2, 3, 4 ..., but i need the date that matches the price like it shows in the View mode in r.

2
Welcome to SO. Can you provide a reproducible example of your data?patL

2 Answers

1
votes

The x in the question is not a data.frame -- it is zoo/xts object, so use write.zoo:

write.zoo(x, "file.csv", sep = ",")
0
votes

It has something to do with how the xts zoo object created by getSymbols stores its row indices. Just convert it to a normal data.frame on export to include the date indices as strings:

ysyms <- getSymbols("SPY",
                    src = "yahoo",
                    from = Sys.Date()-365,
                    to = Sys.Date(),
                    auto.assign = FALSE
                    )

# Convert to data.frame on export.
write.csv(as.data.frame(ysyms), file = "test.csv")