Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

Duplicate rows times count column in r [duplicate]

$
0
0

This question already has an answer here:

I have a table that has been summed by quantity:

Product  Average_Price  Quantity
ABC      25                5
DFG      14                2
HIJ       9                3

I need to duplicate each row as many times as it's total (quantity) value. Like this:

Product Average_Price   Quantity
ABC       25               1
ABC       25               1
ABC       25               1
ABC       25               1 
ABC       25               1
DFG       14               1
DFG       14               1
HIJ        9               1
HIJ        9               1
HIJ        9               1

To make such transformation more explicit, Quantity should now reflect this (1 for each value)

Can this be done in Tidyverse?

Sample table:

df <- data.frame(product = c("ABC", "DFG", "HIJ"),
                 average_price = c(25, 14, 9),
                 quantity = c(5, 2, 3))

Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>