0
votes

I downloaded stock market data from Yahoo (code below) - for context, at first I tried with getSymbols(^DJI) but I got error messages possibly related to Yahoo... different issue.

The point is that once downloaded, and imported into R, I massaged it into a format close enough to a time series to be able to run chartSeries(DJI):

require(RCurl)
require(foreign)
x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv")
DJI <- read.csv(text = x, sep =",")
DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date
rownames(DJI) <- DJI$Date                          # Assigning Date to row names
DJI$Date <- NULL                                   # Removing the Date column
chartSeries(DJI, type="auto", theme=chartTheme('white'))

enter image description here

even if the dataset is not really a time series:

> is.ts(DJI)
[1] FALSE

The problem comes about when I try to find out the date of, for instance, the minimum closing value of the Dow. I can do something like

> DJI[DJI$Close == min(DJI$Close),]
              Open    High     Low   Close Adj.Close   Volume
1985-05-01 1257.18 1262.81 1239.07 1242.05   1242.05 10050000

yielding the entire row, including the row name (1985-05-01), which is the only part I want. However, if I insist on just getting the actual date, I have to juggle a second dataset containing the dates in one of the columns:

require(RCurl)
require(foreign)
x <- getURL("https://raw.githubusercontent.com/RInterested/datasets/gh-pages/%5EDJI.csv")
DJI <- read.csv(text = x, sep =",")

DJI$Date <- as.Date(DJI$Date, format = "%m/%d/%Y") # Formatting Date as.Date
rownames(DJI) <- DJI$Date          # Assigning Date to row names
DJI.raw <- DJI                     # Second dataset for future subsetting
DJI$Date <- NULL                   # Removing the Date column

which does allow me to run

> DJI.raw$Date[DJI.raw$Close == min(DJI.raw$Close)]
[1] "1985-05-01"

Further, I don't think that turning the dataset into an .xts file would help.

1
Please note that when you assign dates to rownames, they turn back to characters. That being said, if you don't want another column in the data.frame containing the dates and you're fine with the dates being character, you can simply do rownames(DJI)[DJI$Close == min(DJI$Close)] - digEmAll
@digEmAll I was under the assumption that a time series requires the row names to be the dates. Isn't this true? Also, I can't run chartSeries() if I keep the Date column within the dataset... - Antoni Parellada
Yes, that's possible (I don't know chartSeries function), but that does not mean you can't have the column in the data.frame anyaway and passing just a columns subset to chartSeries... e.g. chartSeries(DJI[,-1], type="auto", theme=chartTheme('white')) (-1 means we're not passing the column at position 1 to chartSeries, of course if Date column is at position 2, you should pass -2 and so on...) - digEmAll
@digEmAll Is the part about rownames requiring the dates info correct? - Antoni Parellada
I don't know, I don't even know where chartSeries comes from... - digEmAll

1 Answers

0
votes

I'm not clear what you want but it sounds like you just want the date? You mention xts is not an option (which would have been runnable)

time(as.xts(DJI))[which.min(DJI$Close)] # POSIXct format
# [1] "1985-05-01 EDT" 

Otherwise a simple rownames + which.min would get the date for you?

as.Date(rownames(DJI)[which.min(DJI$Close)]) # Date format
# [1] "1985-05-01"