1
votes

I would like to produce dygraph with a data.frame I import from CSV file. I suspect my date column is formatted incorrectly. My date column is originally in %m/%d/%y format.

If applicable, column 1 is class(factor), column 2 and 3 are class(integer). Here is head(mydata)

Date      term1          term2
1 7/1/16   2304              0
2 7/2/16   2304              0
3 7/3/16   1628              0
4 7/4/16   1230              0
5 7/5/16   1216              5
6 7/6/16   2056              0

Here is the dygraph command:

library(tidyverse)
library(dygraphs)
dygraph(mydata, main = "mydata") %>% 
  dyRangeSelector()

I received error: Unsupported type passed to argument 'data'.

I then converted mydata$Date to POSIXct like this:

mydata$DateTime=as.POSIXct(paste(mydata$Date, mydata$Time), format="%Y%m%d %H%M%S")

I expected the above to correct the problem, however I still receive same error. When I view(mydata), I see this:

  Date   term1         term2  DateTime
1 <NA>   2304              0     <NA>
2 <NA>   2304              0     <NA>
3 <NA>   1628              0     <NA>
4 <NA>   1230              0     <NA>
5 <NA>   1216              5     <NA>
6 <NA>   2056              0     <NA>

Clearly, this only worsened the problem.

I was able to use dygraph on imported stock data, and based on head(my stock data) the correct head(mydata) would look like this:

           Date                Open  High  Low    Close  Volume
2016-02-03 2016-02-02 18:00:00 18.00 18.88 16.000 18.20 4157398
2016-02-04 2016-02-03 18:00:00 18.26 19.42 17.570 18.50  469900
2016-02-05 2016-02-04 18:00:00 18.84 18.88 17.520 17.60  219900
2016-02-08 2016-02-07 18:00:00 17.52 18.00 15.720 15.85  372100
2016-02-09 2016-02-08 18:00:00 15.50 15.50 12.748 12.81  744100
2016-02-10 2016-02-09 18:00:00 13.01 14.00 12.790 13.09  260800

Thank you in advance for everyone's time & insight. -M

1
hello you can use mydata$DateTime <- as.POSIXct(as.Date(mydata$DateTime ,format="%m/%d/%y")) - s.brunel
@s.brunel I could not get your command to work, but it may result from my reproducible data/example - it may not accurately reflect my real data. I did however find a solution which I posted below. Thank you. - Madisonel

1 Answers

1
votes
library(zoo)
library(highcharter)
library(xts)

Date=mydata6$Date=as.Date(as.character(mydata6$Date,"%Y-%m-%d"))
Open=mydata6$Open=as.numeric(na.locf(mydata6$Open))
High=mydata6$High=as.numeric(na.locf(mydata6$High))
Z=cbind(Open, High)
newdata=xts(Z,mydata$Date)
dygraph(newdata, main = "Stock") %>% 
  dyRangeSelector()