I am working on incorporating a variable that is recorded once per unit to a yearly dataset. While it is quite straightforward to repeat the observations n times, I have trouble assigning years to the observations.
The structure of my data is as follows:
id startyear endyear dummy
1 1946 2005 1
2 1957 2005 1
3 1982 2005 1
4 1973 2005 1
What I want to do is to create a new row, called years
, which repeats unit 1 n = 2005 - 1946 = 59
times; unit 2 2005-1957
times, and so forth as well as assigning the year, generating the following output:
id startyear endyear dummy year
1 1946 2005 1 1946
1 1946 2005 1 1947
1 1946 2005 1 1948
1 1946 2005 1 1949
[…]
I have attempted to use slice
and mutate
in dplyr, in combination with rep
and seq
but neither gives me the result I want. Any help would be greatly appreciated.