I have the below data:
library(reshape2)
d <- data_frame(
Year = c(2014, 2014, 2015, 2015, 2016),
Country = c("UK", "UK", "UK", "not_UK", "not_UK"),
Gender = c("M", "M", "F", "F", "M"),
Number = c("1", "3", "1", "1", "3"))
I would like to reshape the data in so that the number variable is dis-aggregated.
I have tried to do this using the melt
function as part of reshape2
.
d<- melt(d, id="ID")
This this doesn't give me the desired result. What I would like is for the data to have 9 rows (the sum of 'Number' essentially) and to look like this:
d_disagg <- data.frame(
Year = c(2014, 2014, 2014, 2014, 2015, 2015, 2016, 2106, 2016),
Country = c("UK", "UK", "UK", "UK", "UK", "not_uk", "not_uk", "not_uk",
"not_uk"),
Gender = c("M", "M", "M", "M", "F", "F", "M", "M", "M"))
I think I'm on the right lines using reshape2 and melt - I just can't figure out how to dis-aggregate in the way I would like.
Thanks