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

R. Michael Weylandt michael.weylandt at gmail.com
Thu Aug 2 00:34:58 CEST 2012


Please don't post files to nabble in the future -- when Nabble removes
them, we then have a broken link in the real archives: dput() is much
preferred way to send data.

Anyways:

# mydata <- read.csv("http://r.789695.n4.nabble.com/file/n4638691/jan_2011.csv")
#
# dput(head(mydata, 15))

structure(list(Delivery.Date = structure(c(1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("01/01/2011",
"01/02/2011", "01/03/2011", "01/04/2011", "01/05/2011", "01/06/2011",
"01/07/2011", "01/08/2011", "01/09/2011", "01/10/2011", "01/11/2011",
"01/12/2011", "01/13/2011", "01/14/2011", "01/15/2011", "01/16/2011",
"01/17/2011", "01/18/2011", "01/19/2011", "01/20/2011", "01/21/2011",
"01/22/2011", "01/23/2011", "01/24/2011", "01/25/2011", "01/26/2011",
"01/27/2011", "01/28/2011", "01/29/2011", "01/30/2011", "01/31/2011"
), class = "factor"), Hour.Ending = structure(c(1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L), .Label = c("01:00",
"02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00",
"09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00",
"16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00",
"23:00", "24:00"), class = "factor"), Repeated.Hour.Flag = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "N",
class = "factor"),
    Settlement.Point = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
    8L, 9L, 10L, 11L, 12L, 13L, 14L, 1L), .Label = c("HB_BUSAVG",
    "HB_HOUSTON", "HB_HUBAVG", "HB_NORTH", "HB_SOUTH", "HB_WEST",
    "LZ_AEN", "LZ_CPS", "LZ_HOUSTON", "LZ_LCRA", "LZ_NORTH",
    "LZ_RAYBN", "LZ_SOUTH", "LZ_WEST"), class = "factor"),
Settlement.Point.Price = c(25.18,
    25.36, 25.07, 25.33, 25.12, 24.49, 25.2, 25.12, 25.41, 25.15,
    25.32, 25.32, 25.1, 25.07, 23.84)), .Names = c("Delivery.Date",
"Hour.Ending", "Repeated.Hour.Flag", "Settlement.Point",
"Settlement.Point.Price"
), row.names = c(NA, 15L), class = "data.frame")

mydata[,1] <- as.Date(mydata[,1], format = "%m/%d/%Y")

works just fine for me. Note that you were missing a column.

On Wed, Aug 1, 2012 at 9:21 AM, algotr8der <algotr8der at gmail.com> wrote:
> 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"

No idea where this "ercot" thing comes from.

Best,
Michael

>
> 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