Kuma Raj
2014-Dec-11 15:54 UTC
[R] Create previous dates from date with consideration of leap year
Dear R Community, I wish to create 5 preceding dates from the date variable by ID. How could I create such dates? The code should consider leap year. Thanks Sample data follows: structure(list(id = 1:12, date = structure(c(9L, 6L, 11L, 8L, 7L, 5L, 4L, 3L, 12L, 1L, 10L, 2L), .Label = c("01feb2003", "03mar2008", "04feb2008", "07jul1991", "07jun2010", "13feb2005", "18dec1991", "22sep2005", "27apr1993", "29jan2009", "29may2002", "31jan2005" ), class = "factor"), case = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), .Names = c("id", "date", "case"), class "data.frame", row.names = c(NA, -12L))
Lee, Chel Hee
2014-Dec-11 16:34 UTC
[R] Create previous dates from date with consideration of leap year
The sample data you provided has the variable 'date'. Since the type of this variable is 'factor', you may try to convert type of variable from 'factor' to 'date' as shown in the below: > x$rval <- as.Date(x$date, format="%d%b%Y")-5 > x id date case rval 1 1 27apr1993 1 1993-04-22 2 2 13feb2005 1 2005-02-08 3 3 29may2002 1 2002-05-24 4 4 22sep2005 1 2005-09-17 5 5 18dec1991 1 1991-12-13 6 6 07jun2010 1 2010-06-02 7 7 07jul1991 1 1991-07-02 8 8 04feb2008 1 2008-01-30 9 9 31jan2005 1 2005-01-26 10 10 01feb2003 1 2003-01-27 11 11 29jan2009 1 2009-01-24 12 12 03mar2008 1 2008-02-27 > Leap year is also considered (See the last case 'id=12'). Is this what you are looking for? I hope this helps. Chel Hee Lee On 12/11/2014 9:54 AM, Kuma Raj wrote:> Dear R Community, > > I wish to create 5 preceding dates from the date variable by ID. How > could I create such dates? The code should consider leap year. > > Thanks > > Sample data follows: > > > structure(list(id = 1:12, date = structure(c(9L, 6L, 11L, 8L, > 7L, 5L, 4L, 3L, 12L, 1L, 10L, 2L), .Label = c("01feb2003", "03mar2008", > "04feb2008", "07jul1991", "07jun2010", "13feb2005", "18dec1991", > "22sep2005", "27apr1993", "29jan2009", "29may2002", "31jan2005" > ), class = "factor"), case = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > 1L, 1L, 1L, 1L)), .Names = c("id", "date", "case"), class > "data.frame", row.names = c(NA, > -12L)) > > ______________________________________________ > 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. >
jim holtman
2014-Dec-11 16:40 UTC
[R] Create previous dates from date with consideration of leap year
Here is an example of converting the 'date' your dataframe to 'time' (POSIXct) and then taking the first 'time' and creating the 5 previous days from that date: id date case 1 1 27apr1993 1 2 2 13feb2005 1 3 3 29may2002 1 4 4 22sep2005 1 5 5 18dec1991 1 6 6 07jun2010 1 7 7 07jul1991 1 8 8 04feb2008 1 9 9 31jan2005 1 10 10 01feb2003 1 11 11 29jan2009 1 12 12 03mar2008 1> ?strptime > x$time <- as.POSIXct(x$date, format = "%d%b%Y") > xid date case time 1 1 27apr1993 1 1993-04-27 2 2 13feb2005 1 2005-02-13 3 3 29may2002 1 2002-05-29 4 4 22sep2005 1 2005-09-22 5 5 18dec1991 1 1991-12-18 6 6 07jun2010 1 2010-06-07 7 7 07jul1991 1 1991-07-07 8 8 04feb2008 1 2008-02-04 9 9 31jan2005 1 2005-01-31 10 10 01feb2003 1 2003-02-01 11 11 29jan2009 1 2009-01-29 12 12 03mar2008 1 2008-03-03> seq(x$time[1], by = '-1 day', length = 5)[1] "1993-04-27 EDT" "1993-04-26 EDT" "1993-04-25 EDT" "1993-04-24 EDT" "1993-04-23 EDT">You can do the same thing for the dataframe:> lapply(x$time, function(a) seq(a, by = '-1 day', length = 5))[[1]] [1] "1993-04-27 EDT" "1993-04-26 EDT" "1993-04-25 EDT" "1993-04-24 EDT" "1993-04-23 EDT" [[2]] [1] "2005-02-13 EST" "2005-02-12 EST" "2005-02-11 EST" "2005-02-10 EST" "2005-02-09 EST" [[3]] [1] "2002-05-29 EDT" "2002-05-28 EDT" "2002-05-27 EDT" "2002-05-26 EDT" "2002-05-25 EDT" [[4]] [1] "2005-09-22 EDT" "2005-09-21 EDT" "2005-09-20 EDT" "2005-09-19 EDT" "2005-09-18 EDT" ....... Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it. On Thu, Dec 11, 2014 at 10:54 AM, Kuma Raj <pollaroid at gmail.com> wrote:> Dear R Community, > > I wish to create 5 preceding dates from the date variable by ID. How > could I create such dates? The code should consider leap year. > > Thanks > > Sample data follows: > > > structure(list(id = 1:12, date = structure(c(9L, 6L, 11L, 8L, > 7L, 5L, 4L, 3L, 12L, 1L, 10L, 2L), .Label = c("01feb2003", "03mar2008", > "04feb2008", "07jul1991", "07jun2010", "13feb2005", "18dec1991", > "22sep2005", "27apr1993", "29jan2009", "29may2002", "31jan2005" > ), class = "factor"), case = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, > 1L, 1L, 1L, 1L)), .Names = c("id", "date", "case"), class > "data.frame", row.names = c(NA, > -12L)) > > ______________________________________________ > 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]]