3

How do I create a POSIXct date from the date and time parts below

 date<-as.Date("2014-01-01")
 hour<-5
 minute<-15
 second<-59
 millisecond<-695

 date
 hour
  minute
 second
 millisecond

output should be a POSIXct object

2014-01-01 05:15:59.695

user3022875
  • 8,598
  • 26
  • 103
  • 167
  • 1
    Start by pasting the elements together using `paste()` and then refer to [this Q&A](http://stackoverflow.com/questions/22037657/milliseconds-in-posixct-class) – talat Nov 04 '14 at 17:57
  • Also see `?ISOdatetime` – IRTFM Nov 04 '14 at 20:48

1 Answers1

5

The following should do it: First set options to be able to display milliseconds

options(digits.secs=3)

Next paste the date elements to form a date string

date_string <- paste(date, paste(paste(hour,minute,second,sep=":"),millisecond,sep="."),sep = " ")

Then convert to a POSIXct object

as.POSIXct(date_string,tz="",format="%Y-%m-%d %H:%M:%OS")
Uzma
  • 51
  • 1
  • 2