[R] Using dates in R

William Dunlap wdunlap at tibco.com
Wed Mar 4 19:47:25 CET 2015

You will need to convert strings like "2/15/15" into one of the time/date
classes available in R and then it is easy to do comparisons.  E.g., if you
have no interest in the time of day you can use the Date class:

> d <- as.Date(c("12/2/79", "4/15/15"), format="%m/%d/%y")
> today <- as.Date("2015-03-04") # default format
> d
[1] "1979-12-02" "2015-04-15"
> today
[1] "2015-03-04"
> d < today

The lubridate package contains a bunch of handy functions for manipulating
dates and times.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Wed, Mar 4, 2015 at 6:54 AM, Brian Hamel <bh1605a at student.american.edu>

> Hi all,
> I have a dataset that includes a "date" variable. Each observation includes
> a date in the form of 2/15/15, for example. I'm looking to create a new
> indicator variable that is based on the date variable. So, for example, if
> the date is earlier than today, I would need a "0" in the new column, and a
> "1" otherwise. Note that my dataset includes dates from 1979-2012, so it is
> not one-year (this means I can't easily create a new variable 1-365).
> How does R handle dates? My hunch is "not well," but perhaps there is a
> package that can help me with this. Let me know if you have any
> recommendations as to how this can be done relatively easily.
> Thanks! Appreciate it.
> Best,
> Brian
>         [[alternative HTML version deleted]]
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

	[[alternative HTML version deleted]]

More information about the R-help mailing list