[R] "2013-06-28" coverts to 15884?

Jim Lemon jim at bitwrit.com.au
Thu Mar 20 06:30:13 CET 2014


On 03/20/2014 09:57 AM, Jason Rupert wrote:
> Not sure I follow.
>
> I would like the date formatting information preserved as is implemented
> when using as.character:
> MaxUpdated_row<-NULL
> MaxUpdated_val<- "2013-06-28"
>
> c(MaxUpdated_row, as.character(MaxUpdated_val))
> [1] "2013-06-28"
>
> However, when I try to used as.Date(...) I loose the date formatting
> information:
> c(MaxUpdated_row, as.Date(MaxUpdated_val, "%Y-%m-%d"))
> [1] 15884
>
> I thought as.Date was an instruction describing how the data element
> should be represented?
>
> Maybe as.character is the only way to preserve the "date" formatting
> information in character string format in a vector and then just convert
> it back later on...
>
As I wrote before, all of the data types in a vector object in R must be 
the same. So when you catenate (c) two things that are different data 
types, R coerces them to the same type. Because you are just displaying 
the values in the vector you have created, it doesn't change the data 
type of MaxUpdated_val:

MaxUpdated_val<-as.Date("2013-06-28","%Y-%m-%d")
# prints as a formatted date
MaxUpdated_val
[1] "2013-06-28"
# make a vector with a numeric and MaxUpdated_val
c(0,MaxUpdated_val)
# R coerces the value to numeric and prints both
[1]     0 15884
# now assign it to something
mynewmax<-c(0,MaxUpdated_val)
# in mynewmax the value is numeric
mynewmax
[1]     0 15884
# but MaxUpdated_val is still a date type on its own
MaxUpdated_val
[1] "2013-06-28"

Jim




More information about the R-help mailing list