I have a dataframe like the one below and I would like to select for each id the dates 5, 10 or 15 days prior and posterior to the peak date. How can I do that?
I have tried to use a for loop, but it is only working for one id.
for (i in unique(dataframe$id)){
sub <- dataframe$id[dataframe$id %in% i, ]
sub <- subset(dataframe, date >= peak_date - 2 & date <= peak_date + 2)
}
I have also tried to use the function split but did not manage to manipulate all the individuals in the list (in reality I have about 60 individuals)...
Thank you for your answer.
I would actually like to select all the rows that are present between a number of days before and after "peak date". For example, if I am working on a lag and a lead of 2 days, I would like these dates to be selected:
I think that the function filter will only help me select the date 2 days before and 2 days after but not the dates between them right?