df <- data.frame(n = c(3, 2, 2),
survive = c(2, 1, 2),
a = c(1,1,0),
b = c(0,0,1))
How can I expand the last two columns of the data.frame above, so that each row appears the number of times specified in the column 'n'. And the second column "survive" changes into binary values 0/1 according to the value of "survive"
In other words:
n survive a b
3 2 1 0
2 1 1 0
2 2 0 1
To this
survive a b
1 1 0
1 1 0
0 1 0
1 1 0
0 1 0
1 0 1
1 0 1