Re: [R] as.Date error

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Mon, 18 Apr 2011 06:39:00 -0400

On Apr 17, 2011, at 7:24 PM, Wonjae Lee wrote:

>
> Thank you for replying the as.Date error question.
>
> I have one more question as below.
> I used cbind command, and data x changed, 2010-11-16 to 14929,
> 2010-11-17 to
> 14930.
> What happened to them?
> What should I do to see yyyy-mm-dd format data?
>
>> x=c("11/16/2010","11/17/2010","11/18/2010","11/19/2010")
>> x=as.Date(x,"%m/%d/%Y")
>> x
> [1] "2010-11-16" "2010-11-17" "2010-11-18" "2010-11-19"
>> y=c(1753.75,15077,1759.35,15078)
>> cbind(x,y)
> x y
> [1,] 14929 1753.75
> [2,] 14930 15077.00
> [3,] 14931 1759.35
> [4,] 14932 15078.00
>

cbind.default will return a matrix which needs to have all of its elements of the same type, so your dates were coerced to numeric since their internal representation is as integers.

Had you created x as a data.frame, then cbind would have called cbind.data.frame which was probably what you wanted to happen.

 > xdat <-data.frame(x=as.Date(x,"%m/%d/%Y"))  > cbind(xdat,y)

            x y

1 2010-11-16  1753.75
2 2010-11-17 15077.00
3 2010-11-18  1759.35
4 2010-11-19 15078.00

-- 

David Winsemius, MD
West Hartford, CT

______________________________________________
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.
Received on Mon 18 Apr 2011 - 10:41:00 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 18 Apr 2011 - 10:50:30 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive