Yaro
Yaro

Reputation: 13

Creating a dygraph with time variable

The following is my data set.

Time Sunday Saturday
12:00pm   0.45     0.53
 2:00pm   0.30     0.01
 4:00pm   0.04     0.00
 6:00pm   0.01     0.16
 8:00pm   0.20     0.30

I'm trying to create a dygraph but when I try I get the following:

dygraph(df)
Error in dygraph(df) : Unsupported type passed to argument 'data'.

Basically, my question is how do I convert the Time variable into a variable that can be recognized as time in R.

I tried format="%H:%M" but that results in the variable having NAs.

Upvotes: 1

Views: 2038

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

You have to convert your datatable to a timeseries object. Note that todays date is going to be formatted with strptime. Also note that the timezone is BST

rm(list=ls())
library(dygraphs)
library(xts)
options(stringsAsFactors=F)
my_data <- as.data.frame(cbind(Time=c("12:00pm","2:00pm","4:00pm","6:00pm","8:00pm"), Sunday=c(0.45,0.3,0.04,0.01,0.2),
                               Saturday=c(0.52,0.01,0,0.16,0.3)))
my_data$Time <- strptime(my_data[,1],format="%I:%M %p")
my_data <- xts(my_data[,-1], order.by=my_data[,1])
index(my_data) <- index(my_data)-0
dygraph(my_data)

enter image description here

Upvotes: 3

Related Questions