1

I have a set of data:

ID<-c(111,111,222,222,222,222,222,222)
TreatmentDate<-as.Date(c("2010-12-12","2011-12-01","2009-8-7","2010-5-7","2011-3-7","2011-8-5","2013-8-27","2016-9-3"))
Treatment<-c("AA","BB","CC","DD","AA","BB","BB","CC")
df<-data.frame(ID,TreatmentDate,Treatment)
df

ID   TreatmentDate   Treatment
111   12/12/2010     AA
111   01/12/2011     BB
222   07/08/2009     CC
222   07/05/2010     DD
222   07/03/2011     AA
222   05/08/2011     BB
222   27/08/2013     BB
222   03/09/2016     CC

I also have another dataframe showing the test date for each subject:

UID<-c(111,222)
Testdate<-as.Date(c("2012-12-31","2014-12-31"))
SubjectTestDate<-data.frame(UID,Testdate) 

I am trying to summarise the data such that, say if I want to see how many treatment a subject has prior to their test date, I would get something like this and I would like to export this to a spreasheet.

ID     Prior_to_date      TreatmentAA   TreatmentBB    TreatmentCC    TreatmentDD
111       31/12/2012           1             1              0              0
222       31/12/2014           1             2              1              1

Any help would be much appreciated!!

1 Answer 1

2

We could join the two dataset with 'ID', create a column that checks the condition ('indx'), and use dcast to convert from 'long' to 'wide' format

library(data.table)#v1.9.5+
 dcast(setkey(setDT(df), ID)[SubjectTestDate][,
     indx:=sum(TreatmentDate <=Testdate) , list(ID, Treatment)], 
       ID+Testdate~ paste0('Treatment', Treatment), value.var='indx', length)
#    ID   Testdate TreatmentAA TreatmentBB TreatmentCC TreatmentDD
#1: 111 2012-12-31           1           1           0           0
#2: 222 2014-12-31           1           2           2           1

Update

Based on the modified 'df', we join the 'df' with 'SubjectTestDate', create the 'indx' column as before, and also a sequence column 'Seq', grouped by 'ID' and 'Treatment', use dcast and then remove the duplicated 'ID' rows with unique

  unique(dcast(setkey(setDT(df), ID)[SubjectTestDate][, 
    c('indx', 'Seq') := list(sum(TreatmentDate <= Testdate), 1:.N) ,
    .(ID, Treatment)], ID+ Seq+ Testdate ~ paste0('Treatment', 
            Treatment), value.var='indx', fill=0), by='ID')
  #    ID Seq   Testdate TreatmentAA TreatmentBB TreatmentCC TreatmentDD
  #1: 111   1 2012-12-31           1           1           0           0
  #2: 222   1 2014-12-31           1           2           1           1
11
  • I have installed data.table but it still says "could not find function "dcast"? Wonder if you know what the issue is? May 22, 2015 at 14:59
  • @Headandtoes I am using the devel version of data.table. For other versions, I think dcast.data.table works
    – akrun
    May 22, 2015 at 15:00
  • Thank you so much. Just one more question - is there an easy way to export the data to excel? May 22, 2015 at 15:11
  • 1
    @Headandtoes There are several if you search the web for "write.xls" with "r"
    – Frank
    May 22, 2015 at 15:39
  • Hi, I notice a possible error in the code - If I change one of the treatment dates to after the test date,(e.g. treatment date: 2016-1-31, test date: 2014-12-31) That treatment will still be counted but it should not be counted? May 22, 2015 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.