I want to multiply each row in a dataframe by X times as defined by a value within the row.
Data:
df <- expand.grid(var1 = 1:3, var2 = 1:3)
df$repeatme = c(2, 3, 11, 6, 14, 1, 7, 8, 30)
The multiplying factor is to be determined by the repeatme
variable.
Therefore, I want to multiply :
- row 1 ... 2 times (because
repeatme
== 2); - row 2 ... 3 times (because
repeatme
== 3); - row 3 ... 11 times (because
repeatme
== 11); - etc.
Desired Output:
var1 var2 repeatme
1 1 1 2
2 1 1 2
3 2 1 3
4 2 1 3
5 2 1 3
6 3 1 11
7 3 1 11
8 3 1 11
...
EDIT
To be clear, I'm not asking how to multiply variables within rows! I want to alter the structure/dimensions of the dataframe by multiply the rows themselves!