Rep values from a data frame to another data frame. apply? sapply? <p>I have the following data frame</p> <pre><code>data&lt;-data.frame(ID=c("a", "b", "c", "d"), zeros=c(3,2,5,4), ones=c(1,1,2,1)) ID zeros ones 1 a 3 1 2 b 2 1 3 c 5 2 4 d 4 1 </code></pre> <p>and I wish to create another data frame with 2 columns:</p> <p>First column(id) the ID is repeated (zero+ones) times Second column value should be the c(rep(0, zeros), rep(1, ones))</p> <p>so that the result would be</p> <pre><code> id value 1 a 0 2 a 0 3 a 0 4 a 1 5 b 0 6 b 0 7 b 1 8 c 0 9 c 0 10 c 0 11 c 0 12 c 0 13 c 1 14 c 1 15 d 0 16 d 0 17 d 0 18 d 0 19 d 1 </code></pre> <p>I tried <code>data.frame(id=(rep(data$ID, (data$zeros+data$ones))), value=c(rep(0, data$zeros), rep(1, data$ones)))</code> but doesnt work. Any ideas? Thank you in advance</p>