[R] Simple Date problems with cbind

Tony Plate tplate at acm.org
Tue Jan 30 21:42:54 CET 2007


> It is probably something blindingly simple but can
> anyone suggest something?

You need to use the format code "%Y" for 4-digits years.
You need to create a data frame using 'data.frame()' (cbind() creates a 
matrix when given just vectors).

 > as.Date(c("2005/01/24" ,"2006/01/23" ,"2006/01/23"), "%Y/%m/%d")
[1] "2005-01-24" "2006-01-23" "2006-01-23"
 > data.frame(int=1:3, date=as.Date(c("2005/01/24" ,"2006/01/23" 
,"2006/01/23"), "%Y/%m/%d"))
   int       date
1   1 2005-01-24
2   2 2006-01-23
3   3 2006-01-23
 > (x <- data.frame(int=1:3, date=as.Date(c("2005/01/24" ,"2006/01/23" 
,"2006/01/23"), "%Y/%m/%d")))
   int       date
1   1 2005-01-24
2   2 2006-01-23
3   3 2006-01-23
 > class(x)
[1] "data.frame"
 > sapply(x, class)
       int      date
"integer"    "Date"
 >

-- Tony Plate



More information about the R-help mailing list