Let's say I have the following data frame:
set.seed(1)
df <- data.frame("x" = 1:5, "y" = rnorm(5))
x y
1 1 -0.6264538
2 2 0.1836433
3 3 -0.8356286
4 4 1.5952808
5 5 0.3295078
And I want to duplicate each row by as many times as indicated in x
, as so:
x y
1 1 -0.6264538
2 2 0.1836433
3 2 0.1836433
4 3 -0.8356286
5 3 -0.8356286
6 3 -0.8356286
7 4 1.5952808
8 4 1.5952808
9 4 1.5952808
10 4 1.5952808
11 5 0.3295078
12 5 0.3295078
13 5 0.3295078
14 5 0.3295078
15 5 0.3295078
How would I go about doing that? While my preference is in using a tidyverse solution, I'm open to any other suggestions.