lundi 3 décembre 2018

Reproduce Row based on column value and filling those new rows with other column values in r

I have a table 1 that contains numbers. I would like to create rows that the number of rows comes from Total column. Other column values would be created randomly according to table 1 column values. For instance, table 2 column1 value should has 3 "1" value and rest should be "0". Is there anyone can help me with this?

Table 1

Year    Age Total   column1 column2 column3
2017    15  10         3       4      2

Desired Table

Year    Age Total   column1 column2 column3
2017    15  1          1       0      0
2017    15  1          0       1      0
2017    15  1          0       0      1
2017    15  1          0       0      1
2017    15  1          1       0      0
2017    15  1          0       1      0
2017    15  1          1       0      0
2017    15  1          0       1      0
2017    15  1          0       1      0




Aucun commentaire:

Enregistrer un commentaire