[R] Time conversion

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Thu Oct 2 16:16:57 CEST 2008


> I'm trying to convert a variable that is imported from CSV into a 
datetime,I'm
> trying to use the strptime function but with no joy, can anyone offer 
any
> advise?
> 
> i have a vector
> timestamp
> 07-07-2008-21:03:55
> 07-07-2008-19:52:55
> 07-07-2008-19:54:14
> 07-07-2008-15:24:01
> 07-07-2008-13:39:17
> 07-07-2008-13:39:55
> 
> 
> timestamp<-strptime(timestamp,"%d-%m-%y-%H:%M:%S")
> ## then filter on the datetime
> time<-ifelse(timestamp> "07-08-2008-00:00:00", TRUE, FALSE)

1. Since the years are of the form YYYY, your format vector should be 
"%d-%m-%Y-%H:%M:%S".

2. You don't need the ifelse call if you the last two arguments are just 
TRUE, FALSE

3. You do need to call strptime on your comparision date.

timestamp <- c("07-07-2008-21:03:55",
   "07-07-2008-19:52:55",
   "07-07-2008-19:54:14",
   "07-07-2008-15:24:01",
   "07-07-2008-13:39:17",
   "07-07-2008-13:39:55")
timestamp <- strptime(timestamp,"%d-%m-%Y-%H:%M:%S")
time <- timestamp > strptime("07-08-2008-00:00:00","%d-%m-%Y-%H:%M:%S")

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list