I have data that looks like this (date dates in DD-MM-YYYY format):
ID date drug score
A 28/08/2016 2 3
A 29/08/2016 1 4
A 30/08/2016 2 4
A 2/09/2016 2 4
A 3/09/2016 1 4
A 4/09/2016 2 4
B 8/08/2016 1 3
B 9/08/2016 2 4
B 10/08/2016 2 3
B 11/08/2016 1 3
C 30/11/2016 2 4
C 2/12/2016 1 5
C 3/12/2016 2 1
C 5/12/2016 1 4
C 6/12/2016 2 4
C 8/12/2016 1 2
C 9/12/2016 1 2
For a “drug”: 1 = drug being taken, 2 = drug not being taken.
I need to summarize for each ID:
- 0day: the average score for the days when the drug was taken.
- -1day: the average score for the days preceding when the drug was received.
- + 1 day: the average score for the days immediately after taking the drug.
If the drug was taken 2 days in a row (for example, the last 2 lines of the example), then these points should not be taken into account in the calculations for 1 day or + 1 day (i.e., each of the last two lines would contribute to a 0day score, but would not affect to other indicators).
So, for the data in this example, I need an output table as follows:
-1day 0day +1day
A 3.5 4 4
B 3 3 4
C 3.25 2.5
, 1day + 1day , .
, .
:
, 2day + 2day, .
NoDrug, , 5 .
:
data<-data.frame(ID=c("A","A","A","A","A","A","B","B","B","B","C","C","C","C","C","C","C"),
date=as.Date(c("28/08/2016","29/08/2016","30/08/2016","2/09/2016","3/09/2016","4/09/2016","8/08/2016","9/08/2016","10/08/2016","11/08/2016","30/11/2016","2/12/2016","3/12/2016","5/12/2016","6/12/2016","8/12/2016","9/12/2016"),format= "%d/%m/%Y"),
drug=c(2,1,2,2,1,2,1,2,2,1,2,1,2,1,2,1,1),
score=c(3,4,4,4,4,4,3,4,3,3,4,5,1,4,4,2,2))