Question

Data set i am using is shown below. It is a csv file.

 Date            Subject       val1       val2       val3
    2010-05-01        12         -0.6155     0.5083     2.6286
    2010-06-03        13          0.96416    0.785      1.41
    2010-07-01        14          0.9578     1.258      0.579
    2010-08-01        15          1.249      1.879      0.268

I am trying to visualize the data be gvisMotionchart.

My code is:-

test_motionchart<-read.csv("data.csv")
test_motion=gvisMotionChart(test_motionchart,idvar="Subject",timevar="Date")

After executing this code i get error message which says

Error in testTimevar(x[[options$data$timevar]], options$data$date.format) : 
  The timevar has to be of numeric or Date format. Currently it is  factor

Any help to get out of this.

Thanks in advance

Was it helpful?

Solution

The error tells you exactly what is wrong:

test <- read.table(text="Date            Subject       val1       val2       val3
    2010-05-01        12         -0.6155     0.5083     2.6286
    2010-06-03        13          0.96416    0.785      1.41
    2010-07-01        14          0.9578     1.258      0.579
    2010-08-01        15          1.249      1.879      0.268",header=TRUE)

> str(test$Date)
 Factor w/ 4 levels "2010-05-01","2010-06-03",..: 1 2 3 4

See the Factor w/ 4 levels? You need a Date instead.

Try:

test$Date <- as.Date(test$Date)

Now:

> str(test$Date)
 Date[1:4], format: "2010-05-01" "2010-06-03" "2010-07-01" "2010-08-01"

Alternatively you can specify you want a Date output for the specific variable when reading in the csv using the colClasses option:

test <- read.csv(
filename.csv,
header=TRUE,
colClasses=c(Date="Date")
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top