R - how do I declare a vector of Date? -
for example, tried following create vector of dates, length 5. none work:
date(5) date(5) vector(5, mode = "date" )
this works, wondering if there shortcut?
as.date( numeric( 5 ) )
also, see mode( as.date("2011-01-01" ) ) numeric , understand underlying data structure dates numeric, given vector() has mode , length argument, seems me impossible create vector of date without coercion?
edit
this solution, except length = 0?
date = function( length = 0 ) { newdate = numeric( length ) class(newdate) = "date" return(newdate) }
you can use sequence, or just add:
r> seq( as.date("2011-07-01"), by=1, len=3) [1] "2011-07-01" "2011-07-02" "2011-07-03" r> as.date("2011-07-01") + 0:2 [1] "2011-07-01" "2011-07-02" "2011-07-03" r>
and both work same way nice illustration of why object-orientation nice programming data.
date, saw, has underlying numeric representation (of integers representing number of days since beginning of unix time, aka jan 1, 1970) has class attribute makes formatting, arithmetic, ... behave way utilising dispatch mechanism in r.
edit: same token, can start standard vector , turn date
object:
r> x <- 1:3 r> class(x) <- "date" r> x [1] "1970-01-02" "1970-01-03" "1970-01-04" r>
Comments
Post a Comment