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]]
Your hunch is wrong. Start by typing ?Date at the R prompt. Continue with ?as.Date Then to find out if the date is earlier than today delta <- thedate - Sys.Date() (of course, that will change if you use it tomorrow) Getting your indicator variable can be done very easily with base R; no packages needed. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 3/4/15, 6:54 AM, "Brian Hamel" <bh1605a at student.american.edu> wrote:>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.
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[1] TRUE FALSE 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> wrote:> 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]]
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')[1] FALSE 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 ... P -----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. 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. The contents of this e-mail are confidential and may be ...{{dropped:14}}
Wow! A bold prediction from someone who has done exactly zero investigation of the basic, built-in date/time features in R. Since your example did not include the first two digits of the year, I've used %y instead of %Y. That will assume "19" precedes values from 69-99 and "20" precedes values from 00 to 68. If you decide to implement this with a for loop, it means you have much more to learn.> today <- "3/4/15" > d1 <- "2/15/80" > d2 <- "2/15/16" > # Is d before today, if so 0, otherwise 1 > as.integer(strptime(today, "%m/%d/%y") < strptime(d1, "%m/%d/%y"))[1] 0> as.integer(strptime(today, "%m/%d/%y") < strptime(d2, "%m/%d/%y"))[1] 1 ?strptime for details ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Brian Hamel Sent: Wednesday, March 4, 2015 8:55 AM 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. 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.
Our R-user's group (UC Davis) has a good post on working with dates/times in R: http://www.noamross.net/blog/2014/2/10/using-times-and-dates-in-r---presentation-code.html On Wed, Mar 4, 2015 at 11:08 AM, David L Carlson <dcarlson at tamu.edu> wrote:> Wow! A bold prediction from someone who has done exactly zero > investigation of the basic, built-in date/time features in R. Since your > example did not include the first two digits of the year, I've used %y > instead of %Y. That will assume "19" precedes values from 69-99 and "20" > precedes values from 00 to 68. If you decide to implement this with a for > loop, it means you have much more to learn. > > > today <- "3/4/15" > > d1 <- "2/15/80" > > d2 <- "2/15/16" > > # Is d before today, if so 0, otherwise 1 > > as.integer(strptime(today, "%m/%d/%y") < strptime(d1, "%m/%d/%y")) > [1] 0 > > as.integer(strptime(today, "%m/%d/%y") < strptime(d2, "%m/%d/%y")) > [1] 1 > > ?strptime for details > > ------------------------------------- > David L Carlson > Department of Anthropology > Texas A&M University > College Station, TX 77840-4352 > > > -----Original Message----- > From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Brian > Hamel > Sent: Wednesday, March 4, 2015 8:55 AM > 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. > > 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. > > ______________________________________________ > 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. >-- Myfanwy Johnston Ph.D Candidate, Animal Behavior University of California at Davis Biotelemetry Lab +1.530.205.5243 [[alternative HTML version deleted]]
> today <- as.Date("2015-03-04") # default formatBetter is: today <- Sys.Date() S -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of William Dunlap Sent: Thursday, 5 March 2015 7:47a To: Brian Hamel Cc: r-help at r-project.org Subject: Re: [R] Using dates in R 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[1] TRUE FALSE 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> wrote:> 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]] ______________________________________________ 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.