david.schruth
2010-Feb-23 19:18 UTC
[R] Casting "character" to "Date" using the "as" function
Hello, I'm trying to write a function which, among other things, attempts to convert variables of type 'character' into various other classes like 'integer', 'numeric', 'character' & "Date", depending on the class of a second parameter to this same function. Converting from character to Date is no problem with the "as.Date" function:> date.var.char <- as.character("2009-10-21") > as.Date(date.var.char)[1] "2010-02-23" But when I try to do the equivalent using just "as" I get an error> date.var.Date <- as.Date("2009-10-21") > as(date.var.char, class(date.var.Date))Error in as("2009-10-21", "Date"): no method or default for coercing "character" to "Date" The same is true for POSIXt, POSIXlt, & POSIXct Is there something that I can do without writing those extra 2 lines of code to check and see if 'date.var.Date' is actually of the 'Date' class? Or is this a bug? Thanks, Dave
Seemingly Similar Threads
- as.matrix.data.frame() in R 1.9.0 converts to character when it should (?) convert to numeric
- Date arithmetic coerces POSIXlt to POSIXct?
- Dates as POSIXt
- Wishlist: In documentation, say that `+.Date`(Date, difftime) should be called directly or remove 'or an object of class "difftime"' (PR#14072)
- Suggestions for improvement as regards `as` methods, and a call for consistency in `as.Date` methods