Skip to content

Format about Date and time

3 messages · Aimin Yan, David Winsemius, Gabor Grothendieck

#
I have a data set like this:

 > head(FormatedData)
     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

I wan to convert the format of Date and Time, so I did this:

pdate <- as.POSIXct(strptime(FormatedData$Date,"%m/%d/%Y"))
ptime <- as.POSIXct(strptime(FormatedData$Time,"%H:%M:%S"))

but I get the following format:

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

in ptime, it includes the current date, I don't want this,
actually, I want to have the following formats:

     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

Does anyone know how to do these?
#
On Mar 14, 2009, at 8:52 AM, Aimin Yan wrote:

            
You do not have a %Y format for years.

See if this minimal example helps::
timedf <- data.frame(Date1 = "12/20/08", Time1= "17:03:00")
strptime( with(timedf, paste(Date1,Time1) ), format="%m/%d/%y %H:%M:%S")
# [1] "2008-12-20 17:03:00"
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
#
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: