I know from this answer how to duplicate rows of a dataframe. That's fine if you want to repeat the rows n times.
I want to do something similar but to also add a prefix within a column of newly added rows. Note: I am not basing the repetition count on the number of rows. It needs to be provided by a parameter (call it k).
So the operation should look like this assuming I want to repeat the dataframe k=3 times:
Input:
data.frame(a = c("1","2","3", "4"),b = c(1,2,3, 4))
a b
1 "1" 1
2 "2" 2
3 "3" 3
4 "4" 4
Output:
a b
1 "1" 1
2 "2" 2
3 "3" 3
4 "4" 4
5 "1_1" 1
6 "1_2" 2
7 "1_3" 3
8 "1_4" 4
9 "2_1" 1
10 "2_2" 2
11 "2_3" 3
12 "2_4" 4
What's a nice R way to do this??