[R] Using dates in R

Peter Alspach Peter.Alspach at plantandfood.co.nz
Wed Mar 4 19:59:05 CET 2015

Tena koe Brian

See ?as.Date and ?strptime (and, maybe, ?locales).  For example:

as.Date('2/15/15', '%m/%d/%y')
[1] "2015-02-15"

as.Date('12/15/14', '%m/%d/%y') < as.Date('2/15/15', '%m/%d/%y')
[1] TRUE
> as.Date('12/15/16', '%m/%d/%y') < as.Date('2/15/15', '%m/%d/%y')

You might have problems across the century boundary with only two digits in the year, but ...

> as.Date('12/15/79', '%m/%d/%y') < as.Date('2/15/15', '%m/%d/%y')
[1] TRUE

HTH ...


-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Brian Hamel
Sent: Thursday, 5 March 2015 3:55 a.m.
To: r-help at r-project.org
Subject: [R] Using dates in R

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.


	[[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.
The contents of this e-mail are confidential and may be ...{{dropped:14}}

More information about the R-help mailing list