[R] Format about Date and time
David Winsemius
dwinsemius at comcast.net
Sat Mar 14 16:26:23 CET 2009
On Mar 14, 2009, at 8:52 AM, Aimin Yan wrote:
> 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"))
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"
>
> 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?
>
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
More information about the R-help
mailing list