I want to make an empty vector of POSIXct so that I can put a POSIXct in it:
vec <- vector(\"POSIXct\", 10)
vec
vec[1] <- \"2014-
I use the following function for this. Very similar to the other solutions.
vector_datetime <- function(n = 0L) structure(rep(NA_integer_, n), class = c("POSIXct", "POSIXt"))
So you can do things like this.
> vector_datetime()
POSIXct of length 0
> vector_datetime(10)
[1] NA NA NA NA NA NA NA NA NA NA
> class(vector_datetime(10))
[1] "POSIXct" "POSIXt"
This can also be done with lubridate.
library(lubridate)
> as_datetime(integer(0))
POSIXct of length 0