Convert Wide to Long with frequency column

Viewed 889

I am trying to convert my data.frame from a wide table to a long table with a frequency column.

data("UCBAdmissions")
ucb_admit <- as.data.frame(UCBAdmissions)
ucb_admit
   Admit    Gender Dept Freq
1  Admitted   Male    A  512
2  Rejected   Male    A  313
3  Admitted Female    A   89
4  Rejected Female    A   19
...  

And I want to gather this data (tidyr package, similar to melt from reshape) but use the Freq to specify how many times the row should be repeated.

My target data would therefore look something like:

     Admit      Gender Dept
1    Admitted   Male    A
2    Admitted   Male    A
3    Admitted   Male    A
4    Admitted   Male    A
5    Admitted   Male    A
6    Admitted   Male    A
...
4523 Rejected Female    F
4524 Rejected Female    F
4525 Rejected Female    F
4526 Rejected Female    F

I would like to use the tidyr::gather() to do this, however my results are not correct as I am unsure if/how to include the Freq column?

Thanks

2 Answers
Related