How do I create an empty vector of dates?

Using the lubridate package and deciding what your intended format will be (eg. yyyy/mm/dd) you can use:

lubridate::ymd()
# or
lubridate::mdy()

etc.


Just add a class attribute to a length-0 interger and it's a Date (at least it will appear to be one if you extend it with values that are sensible when interpreted as R-Dates):

>  x <- integer(0)
> x
integer(0)
> class(x) <- "Date"
> x
character(0)
> class(x)
[1] "Date"
> y <- c(x, 1400)
> y
[1] "1973-11-01"

The output from as.Date happens to be a character value so the first call is a bit misleading.

> as.Date(integer())
character(0)
> str( as.Date(integer()) )
Class 'Date'  num(0) 
> dput( as.Date(integer()) )
structure(numeric(0), class = "Date")

With recent versions of R, you have to supply the origin :

as.Date(x = integer(0), origin = "1970-01-01")

Tags:

R

Vector