I have a data based on different years, repeated several time. I want my output having columns equal to number of years, each column for one year. Now, the purpose is to create dummy for each year separately. For example, the output column for year 2000 must have a value "1" whenever there is a non-NA observation in the main data parallel to year 2000, else "0". Moreover, NA must remain NA. Please see below a small sample of input data:
df:
2000 NA
2001 NA
2002 -1.3
2000 1.1
2001 0
2002 NA
2000 -3
2001 3
2002 4.1
Now the output should be:
df1:
2000 2001 2002
NA NA NA
NA NA NA
0 0 1
1 0 0
0 1 0
NA NA NA
1 0 0
0 1 0
0 0 1
I would prefer to obtain this output by using a "for loop", if possible. Otherwise, any simpler approach will be appreciated.