How do I find out when people have moved addresses, or when rows are different, in R?

Viewed 45

I have a data frame that looks like this:

state  address    id month 
MA       road1    1    02
CA       road2    2    03
TX       road3    2    02
MA       road1    1    03

And I want 2 data frames: First:

id moved  moved_on_date moved_to   moved_from
1   0           NA       NA         NA
2   1          02        road2      road3

where moved ==1 if address in the prior month =/= address the next month

and data frame number 2

state  gain loss gain.from.tx loss.to.ca date  etc
MA     0      0     0               0     02
CA     1      0     1               0     02
TX     0      1     0               1     02

Any ideas?

0 Answers
Related