[R] modify timestemp

Enrico Schumann es at enricoschumann.net
Thu Jul 4 11:34:03 CEST 2013


On Wed, 03 Jul 2013, Ye Lin <yelin at lbl.gov> writes:

> Hey All,
>
> I want to standardize my timestamp which is formatted as hh:mm:ss
>
>  My data looks like this:
>
>      Date     Time
> 01/01/2013 00:09:01
> 01/02/2013 00:10:14
> 01/03/2013 00:11:27
> 01/04/2013 00:12:40
> 01/05/2013 00:13:53
> 01/06/2013 00:15:06
> 01/07/2013 00:16:19
> 01/08/2013 00:17:32
> 01/09/2013 00:18:45
> 01/10/2013 00:19:58
>
> Dataset <- structure(list(Date = c("01/01/2013", "01/02/2013",
> "01/03/2013",
> "01/04/2013", "01/05/2013", "01/06/2013", "01/07/2013", "01/08/2013",
> "01/09/2013", "01/10/2013"), Time = c("00:09:01", "00:10:14",
> "00:11:27", "00:12:40", "00:13:53", "00:15:06", "00:16:19", "00:17:32",
> "00:18:45", "00:19:58")), .Names = c("Date", "Time"), class = "data.frame",
> row.names = c(NA,
> -10L))
>
> I would like to change all the records in "Time" column uniformed as
> hh:mm:00, then the output would be this:
>
> Date     Time
> 01/01/2013 00:09:00
> 01/02/2013 00:10:00
> 01/03/2013 00:11:00
> 01/04/2013 00:12:00
> 01/05/2013 00:13:00
> 01/06/2013 00:15:00
> 01/07/2013 00:16:00
> 01/08/2013 00:17:00
> 01/09/2013 00:18:00
> 01/10/2013 00:19:00
>
> Thanks for your help!

Since your dates and times are character vectors, you can do this:
 
  substr(Dataset$Time, 7, 8) <- "00"

If you want to treat them as actual dates and times, you need to convert
them into one of R's date/time classes (see ?DateTimeClasses):

  timestamp <- as.POSIXlt(paste(Dataset$Date, Dataset$Time), 
                          format = "%m/%d/%Y %H:%M:%S")

  timestamp$sec

  ## [1]  1 14 27 40 53  6 19 32 45 58

  timestamp$sec <- 0


>
> 	[[alternative HTML version deleted]]
>

Please send only plain-text mails to this list.



-- 
Enrico Schumann
Lucerne, Switzerland
http://enricoschumann.net



More information about the R-help mailing list