How do I create an empty vector of dates?

Is it possible to create an empty vector of dates in r?

I can create an empty vector of integers, paired, boolean, etc .:

> integer()
integer(0)
> double()
numeric(0)
> logical()
logical(0)
> length(integer())
[1] 0

      

but this meme does not work for dates as it date()

returns the current system date and time. So how can I create an emtpy vector of dates?

+3


source to share


3 answers


Just add the class attribute to the time interval -0 and date (at least it will show up as one if you extend it with values ​​that are reasonable when interpreting 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 exit as.Date

turns out to be a symbolic value, so the first call is a little misleading.

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

      

+6


source


In recent versions of R, you need to provide the source:



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

      

+5


source


Using the lubridate package and having determined what your intended format will be (e.g. yyyy / mm / dd), you can use:

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

      

and etc.

0


source







All Articles