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") > x id 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 <pollar...@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@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@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.