I have a data.table in R that contains multiple status values for each user collected at different time points. I want to compare the the status values at consecutive time points and update the rows with a flag whenever the status changes. Please see below for an example
DT_A <- data.table(sid=c(1,1,2,2,2,3,3), date=as.Date(c("2014-06-22","2014-06-23","2014-06-22","2014-06-23", "2014-06-24","2014-06-22","2014-06-23")), Status1 = c("A","B","A","A","B","A","A"), Status2 = c("C","C","C","C","D","D","E"))
DT_A_Final <- data.table(sid=c(1,1,2,2,2,3,3), date=as.Date(c("2014-06-22","2014-06-23","2014-06-22","2014-06-23", "2014-06-24","2014-06-22","2014-06-23")), Status1 = c("0","1","0","0","1","0","0"), Status2 = c("0","0","0","0","1","0","1"))
The original data table DT_A is
sid date Status1 Status2
1 1 2014-06-22 A C
2 1 2014-06-23 B C
3 2 2014-06-22 A C
4 2 2014-06-23 A C
5 2 2014-06-24 B D
6 3 2014-06-22 A D
7 3 2014-06-23 A E
The final required data table is DT_A_final
sid date Status1 Status2
1 1 2014-06-22 0 0
2 1 2014-06-23 1 0
3 2 2014-06-22 0 0
4 2 2014-06-23 0 0
5 2 2014-06-24 1 1
6 3 2014-06-22 0 0
7 3 2014-06-23 0 1
Please help how I can I achieve this?