How to initialize data.frame with column of type POSIXct?

JerryWho picture JerryWho · Jun 16, 2014 · Viewed 9.1k times · Source

I can initialize a data.frame via

df <- data.frame(a=numeric(), b=character())

But how do I define a column of type POSIXct?

df <- data.frame(a=numeric(), b=character(), c=POSIXct())

won't work.

Answer

konvas picture konvas · Jun 23, 2014

You can try

df <- data.frame(a=numeric(), b=character(), c=as.POSIXct(character()))

Similarly, you can create a POSIXct column of NAs in a data frame with > 0 rows by creating a new column with as.POSIXct(NA).