[R] as.date: do not know how to convert 'test[1]' to class "Date"

Rui Barradas ruipbarradas at sapo.pt
Thu Aug 2 00:40:36 CEST 2012


Hello,

Try reading the data file with stringsAsFactors = FALSE, like this you 
don't have character vectors, you have factors, which are coded as 
consecutive integers 1, 2, etc.

?read.table
?read.csv

See in particular the argument above, stringsAsFactors

Hope this helps,

Rui Barradas

Em 01-08-2012 15:21, algotr8der escreveu:
> I have an object, which I pull in from a csv file here
> http://r.789695.n4.nabble.com/file/n4638691/jan_2011.csv jan_2011.csv
>
> mydata <- read.csv("jan_2011.csv", header=TRUE, sep=",")
>
>> head(mydata)
>    Delivery.Date Hour.Ending Repeated.Hour.Flag Settlement.Point
> Settlement.Point.Price
> 1    01/01/2011       01:00                  N        HB_BUSAVG
> 25.18
> 2    01/01/2011       01:00                  N       HB_HOUSTON
> 25.36
> 3    01/01/2011       01:00                  N        HB_HUBAVG
> 25.07
> 4    01/01/2011       01:00                  N         HB_NORTH
> 25.33
> 5    01/01/2011       01:00                  N         HB_SOUTH
> 25.12
> 6    01/01/2011       01:00                  N          HB_WEST
> 24.49
>
>> str(mydata)
> 'data.frame':	10416 obs. of  5 variables:
>   $ Delivery.Date         : Factor w/ 31 levels "01/01/2011","01/02/2011",..:
> 1 1 1 1 1 1 1 1 1 1 ...
>   $ Hour.Ending           : Factor w/ 24 levels "01:00","02:00",..: 1 1 1 1 1
> 1 1 1 1 1 ...
>   $ Repeated.Hour.Flag    : Factor w/ 1 level "N": 1 1 1 1 1 1 1 1 1 1 ...
>   $ Settlement.Point      : Factor w/ 14 levels "HB_BUSAVG","HB_HOUSTON",..:
> 1 2 3 4 5 6 7 8 9 10 ...
>   $ Settlement.Point.Price: num  25.2 25.4 25.1 25.3 25.1 ...
>
> I want to convert the Delivery.Date field to a date object. I tried various
> attempts but failed with the following:
>
>> as.Date(mydata[1], "%m/%d/%Y")
> Error in as.Date.default(ercot[1], "%m/%d/%Y") :
>    do not know how to convert 'ercot[1]' to class "Date"
>
> I even tried to save the first column to a separate object and tried the
> same but got the same result.  At this point I'm not sure how to move
> forward. Appreciate the help.
>
>
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/as-date-do-not-know-how-to-convert-test-1-to-class-Date-tp4638691.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list