I have a data frame representing 15 years of monitoring several hundred patients. I want to create a subset of a data frame, including the last 12 months of data for each patient.
The following is a representative example of my data (including one missing value as there is no missing data in my actual dataset):
example.dat <- data.frame(
ID = c(1,1,1,1,2,2,2,3,3,3),
Date = as.Date(c("2000-02-01", "2004-10-21", "2005-02-06",
"2005-06-14", "2002-11-24", "2009-03-05",
"2009-07-20", "2005-09-02", "2006-01-15",
"2006-05-18")),
Cat = c("Yes", "Yes", "No", "Yes", "No",
"Yes", "Yes", NA, "No", "No")
)
example.dat
Which gives the following result:
ID Date Cat
1 1 2000-02-01 Yes
2 1 2004-10-21 Yes
3 1 2005-02-06 No
4 1 2005-06-14 Yes
5 2 2002-11-24 No
6 2 2009-03-05 Yes
7 2 2009-07-20 Yes
8 3 2005-09-02 <NA>
9 3 2006-01-15 No
10 3 2006-05-18 No
I need to figure out how a subset, for each ID number, the most recent record and all records for the previous 12 months.
ID Date Cat
2 1 2004-10-21 Yes
3 1 2005-02-06 No
4 1 2005-06-14 Yes
6 2 2009-03-05 Yes
7 2 2009-07-20 Yes
8 3 2005-09-02 <NA>
9 3 2006-01-15 No
10 3 2006-05-18 No
R, , (( ) - ( )).