I am new to R and I have a problem at hand. I basically want to create a dataframe that contains a dummy variable for every year somebody has had a house. 0 for every year he did not sell, and 1 for the year he did sell. Next to this I need the year iteslf, and the year the house was bought in, because I have a seperate dataset with the housing price index for every year. I guess this would be done best with some sort of loop, since I have 40.000 transactions
Example data I have now
Buy | Sold |
---|---|
1620 | 1624 |
1622 | 1628 |
What I then need it to become
dummy | year bought | current year |
---|---|---|
0 | 1620 | 1621 |
0 | 1620 | 1622 |
0 | 1620 | 1623 |
1 | 1620 | 1624 |
0 | 1622 | 1623 |
0 | 1622 | 1624 |
0 | 1622 | 1625 |
0 | 1622 | 1626 |
0 | 1622 | 1627 |
1 | 1622 | 1628 |
Then ultimately I also need another column with the price difference between the housing price index in current year - the price index of the year the house was bought. I do have a seperate dataset with the price index for each year. I don't know how to do that but I guess that would be relatively easy once I have this data figured out. Thanks in advance!