1
votes

I have a dataframe that looks like this

id year changetype
 1  2010         1
 1  2012         2
 2  2014         2
 2  2014         2
 3  2012         1
 3  2012         2
 3  2014         2
 3  2014         1

I want to get something like this

id year changetype
 1  2010         1
 1  2012         2
 2  2014         2
 2  2014         2

In other words I want to remove all observations associated with id 3 because, in the same year (2012) id=3 presents both changetype=1 and changetype=2.

How can I impose a condition on variable for grouped observation by id and year? Many thanks to everyone helping me.

1

1 Answers

0
votes

You can use data.table package to achieve this-

library(data.table)
setDT(dt)
dt[,count:=lapply(.SD,function(x)length(unique(x))), by=.(id,year)]
dt[,keep:=uniqueN(count), by=id][keep==1,.(id,year,changetype)]

   id year changetype
1:  1 2010          1
2:  1 2012          2
3:  2 2014          2
4:  2 2014          2