[R] Date/Time to date & time

jim holtman jholtman at gmail.com
Sun Sep 20 22:24:35 CEST 2009


Here is one way to do it.  Not sure why you want columns with either
date or time since you already have them.  This will create a POSIXct
object you can use for processing and then two character columns with
date and time.  Exactly what are you going to do with the data.

> str(x)
'data.frame':   7 obs. of  4 variables:
 $ V1: int  1 2 3 4 5 6 7
 $ V2: chr  "3/23/2009" "3/23/2009" "3/23/2009" "3/23/2009" ...
 $ V3: chr  "6:30:00" "6:30:00" "6:39:00" "6:39:00" ...
 $ V4: chr  "AM" "AM" "AM" "AM" ...
> y <- x  # use temporary so we can try again
> y$dateTime <- as.POSIXct(paste(y[[2]], y[[3]], y[[4]]), format="%m/%d/%Y %I:%M:%S %p")
> # add columns with only date and time
> y$date <- format(y$dateTime, "%m/%d/%Y")
> y$time <- format(y$dateTime, "%I:%M:%S %p")
> str(y)
'data.frame':   7 obs. of  7 variables:
 $ V1      : int  1 2 3 4 5 6 7
 $ V2      : chr  "3/23/2009" "3/23/2009" "3/23/2009" "3/23/2009" ...
 $ V3      : chr  "6:30:00" "6:30:00" "6:39:00" "6:39:00" ...
 $ V4      : chr  "AM" "AM" "AM" "AM" ...
 $ dateTime: POSIXct, format: "2009-03-23 06:30:00" "2009-03-23
06:30:00" "2009-03-23 06:39:00" "2009-03-23 06:39:00" ...
 $ date    : chr  "03/23/2009" "03/23/2009" "03/23/2009" "03/23/2009" ...
 $ time    : chr  "06:30:00 AM" "06:30:00 AM" "06:39:00 AM" "06:39:00 AM" ...
> y
  V1        V2      V3 V4            dateTime       date        time
1  1 3/23/2009 6:30:00 AM 2009-03-23 06:30:00 03/23/2009 06:30:00 AM
2  2 3/23/2009 6:30:00 AM 2009-03-23 06:30:00 03/23/2009 06:30:00 AM
3  3 3/23/2009 6:39:00 AM 2009-03-23 06:39:00 03/23/2009 06:39:00 AM
4  4 3/23/2009 6:39:00 AM 2009-03-23 06:39:00 03/23/2009 06:39:00 AM
5  5 3/23/2009 6:48:00 AM 2009-03-23 06:48:00 03/23/2009 06:48:00 AM
6  6 3/23/2009 6:48:00 AM 2009-03-23 06:48:00 03/23/2009 06:48:00 AM
7  7 3/23/2009 7:00:00 AM 2009-03-23 07:00:00 03/23/2009 07:00:00 AM
>


On Sun, Sep 20, 2009 at 4:11 PM, Mark Knecht <markknecht at gmail.com> wrote:
> Hi,
>    Can strptime (or some other function) help me turn the following
> column of a data.frame into two new columns, one as date and the other
> as time, preserving the AM/PM value?
>
> Thanks,
> Mark
>
>> B
>              ENTRY DATE
> 1  3/23/2009 6:30:00 AM
> 2  3/23/2009 6:30:00 AM
> 3  3/23/2009 6:39:00 AM
> 4  3/23/2009 6:39:00 AM
> 5  3/23/2009 6:48:00 AM
> 6  3/23/2009 6:48:00 AM
> 7  3/23/2009 7:00:00 AM
>
>> strftime(B, tz="", format="%m/%d/%Y %I:%M:%s %p")
> Error in as.POSIXlt.default(x, tz = tz) :
>  do not know how to convert 'x' to class "POSIXlt"
>>
>
>> strptime(B, format="%m/%d/%Y %I:%M:%s %p")
> [1] NA
>>
>
>> mode(B)
> [1] "list"
>> class(B)
> [1] "data.frame"
>>
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list