Skip to content
Prev 173741 / 398503 Next

Format about Date and time

library(chron)
Lines <- " ID Target Actual     Date     Time
1 2030      0  -57.0 12/20/08 17:03:00
2 2030     90   90.0 12/20/08 18:41:00
3 2030     45   43.8 12/21/08 14:36:00
4 2030      0  -23.8 12/21/08 19:30:00
5 2030     90   90.2 12/21/08 21:48:00
6 2030     45   48.6 12/22/08 13:02:00"

DF <- read.table(textConnection(Lines), header = TRUE, as.is = TRUE)
dt <- chron(DF$Date, DF$Time)
DF$pdate <- format(as.Date(dt))
DF$ptime <- format(dt - dates(dt))

This gives:
ID Target Actual     Date     Time      pdate    ptime
1 2030      0  -57.0 12/20/08 17:03:00 2008-12-20 17:03:00
2 2030     90   90.0 12/20/08 18:41:00 2008-12-20 18:41:00
3 2030     45   43.8 12/21/08 14:36:00 2008-12-21 14:36:00
4 2030      0  -23.8 12/21/08 19:30:00 2008-12-21 19:30:00
5 2030     90   90.2 12/21/08 21:48:00 2008-12-21 21:48:00
6 2030     45   48.6 12/22/08 13:02:00 2008-12-22 13:02:00

See R News 4/1 for info on dates.

Also, why do you want to store formatted dates in the first place?
On Sat, Mar 14, 2009 at 8:52 AM, Aimin Yan <aiminy at iastate.edu> wrote: