I have a some what same question as Subsetting a data.table using another data.table and Subset a data.table by matching columns of another data.table
dt is the same.
dt
id year event
1: 2 2005 1
2: 2 2006 1
3: 2 2007 1
4: 4 2008 1
5: 4 2009 1
6: 2 2005 0
7: 4 2006 0
8: 4 2007 0
9: 2 2008 0
dt <- data.table(id = c(2,2,2,4,4,2,4,4,2), year = c(2005:2009,2005:2008),
event = rep(1:0, times=c(5, 4)))
But, the dt1 is a little bit different
dt1
year performance event
1: 2005 1000 1
2: 2006 1001 1
3: 2007 1002 1
4: 2008 1003 1
5: 2009 1004 1
6: 2005 1005 0
7: 2006 1006 0
8: 2007 1007 0
9: 2008 1008 0
dt1 <- data.table(year = c(2005:2009,2005:2008), performance = 1000:1008,
event = rep(1:0, times=c(5, 4)))
I want to split dt1 based on dt's id and group by event. The desired output would like to be two different data.tables:
dt1.sub1
year performance event
1: 2005 1000 1
2: 2006 1001 1
3: 2007 1002 1
4: 2005 1005 0
5: 2008 1008 0
dt1.sub2
year performance event
1: 2008 1003 1
2: 2009 1004 1
3: 2006 1006 0
4: 2007 1007 0
Is there a way to achieve this without using merge?
dtanddtexceptdthas an additionalidcolumn. And I want to splitdt1based ondt'sid.