[R] Format about Date and time

Gabor Grothendieck ggrothendieck at gmail.com
Sat Mar 14 17:32:07 CET 2009


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:

> DF
    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:
> 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?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list