« R wins 2009 KDD Cup "slow challenge" | Main | Implementing Decision Tree Bagging models in R: A Walkthrough »

June 02, 2009


Feed You can follow this conversation by subscribing to the comment feed for this post.

And when you think you have it all figured out do (inspired by help("DateTimeClasses")):

> d <- c("2005-12-31 23:59:59", "2005-12-31 23:59:60", "2006-01-01 00:00:00")
> as.POSIXct(d,tz="UTC")
[1] "2005-12-31 23:59:59 UTC" "2006-01-01 00:00:00 UTC"
[3] "2006-01-01 00:00:00 UTC"
> as.POSIXlt(d,tz="UTC")
[1] "2005-12-31 23:59:59 UTC" "2005-12-31 23:59:60 UTC"
[3] "2006-01-01 00:00:00 UTC"


Also note that DST rules are implemented by your operating system so if you upgrade your system the external representation of POSIXct objects may change. I spent a couple of weeks debugging this once. Use POSIXlt for legal dates (“the contract/assembly line/whatever starts...”) and POSIXct for events in the universe (asteroid impacts, say).

Try tz="PST8PDT" for the daylight savings time in the pacific time zone. On my mac you can see all the available time zones in the /usr/share/zoneinfo directory, I'm pretty sure that would be similar on most unix based systems. Not sure where you would find the list on Windows.

Also note this is highly dependent on the sytem you are using the code on so this is not really portable code. I usually store everything as GMT to try and keep things constistent and as portable as possible. Also be aware of the data you are getting, I wasted a day on finding out a data set was stored strictly in PST and never adjusted for daylight savings once.

I want to find a way to find a maximum value between two columns (a,b) and create a third column in a dataframe c = max(a,b). See example below.

I tried using apply but it does not work because it coerces the data into a matrix/character conversion. How can this be done?

> mydf=data.frame(a=rep(Sys.time()+rnorm(1),10), b=rep(Sys.time(), 10))
> mydf
a b
1 2009-06-26 08:53:36 2009-06-26 08:53:35
2 2009-06-26 08:53:36 2009-06-26 08:53:35
3 2009-06-26 08:53:36 2009-06-26 08:53:35
4 2009-06-26 08:53:36 2009-06-26 08:53:35
5 2009-06-26 08:53:36 2009-06-26 08:53:35
6 2009-06-26 08:53:36 2009-06-26 08:53:35
7 2009-06-26 08:53:36 2009-06-26 08:53:35
8 2009-06-26 08:53:36 2009-06-26 08:53:35
9 2009-06-26 08:53:36 2009-06-26 08:53:35
10 2009-06-26 08:53:36 2009-06-26 08:53:35

That's awesome! Exactly the code I was looking for! Thanks, dude!

Great post.
I wanted to add a couple of comments for reference by others or possibly myself in the future.

I think it would be handy to have a reference that shows how to convert times between time zones, or how to calculate the difference from GMT to the local time zone.

I would be interested to know if there are more elegant ways to accomplish any of these.

## How to calculate the difference in local time from UTC
as.POSIXct(format(Sys.time(), tz = "GMT")) -
as.POSIXct(format(Sys.time(), tz = ""))

## How to convert a time that was local to GMT
now = Sys.time()
nowAsCharacter = format(now, tz="GMT")
as.POSIXct(format(nowAsCharacter, tz="GMT"), tz="GMT")

## This is a reminder that the lt only matters when converting time zones
as.POSIXct(Sys.time(), tz = "GMT")
as.POSIXlt(Sys.time(), tz = "GMT")

as.POSIXct(Sys.time(), tz = "")
as.POSIXlt(Sys.time(), tz = "")

Thanks, Gene! That's the best way I know of to convert time zones -- anyone have any other suggestions?

Use lubridate

now = Sys.time()
with_tz(now, 'GMT')

The comments to this entry are closed.

Search Revolutions Blog

Got comments or suggestions for the blog editor?
Email David Smith.
Follow revodavid on Twitter Follow David on Twitter: @revodavid
Get this blog via email with Blogtrottr