0

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

Mrmoleje
  • 453
  • 1
  • 12
  • 35
  • Sorry let me edit. Posted the wrong dataframe – Mrmoleje Sep 06 '18 at 09:30
  • 1
    @RonakShah now edited with the correct dataframe – Mrmoleje Sep 06 '18 at 09:32
  • 4
    Possible duplicate of [Replicate each row of data.frame and specify the number of replications for each row](https://stackoverflow.com/questions/2894775/replicate-each-row-of-data-frame-and-specify-the-number-of-replications-for-each) – Ritchie Sacramento Sep 06 '18 at 09:41
  • Look at here: https://rdrr.io/cran/splitstackshape/man/expandRows.html – Saurabh Chauhan Sep 06 '18 at 09:46
  • Both of those work on the small data set I have posted above but when I apply to my data set (500,000 plus wors i get the following error `Error in rep(seq_nrow(data), w) : invalid 'times' argument` Any idea why i might be getting that? – Mrmoleje Sep 06 '18 at 10:04
  • I've noticed that the data I am working on has '0' values in the 'Number' column, maybe you can't have anything less than 1 for using these functions. i assume R wouldn't know what to do with a 0 – Mrmoleje Sep 06 '18 at 10:07

0 Answers0