I am trying to manipulate a wide table which represents the per cent of each household composition type within two towns to a long-form table (basically, a reverse of a pivot table).
In the long table, I would like each row to represent the household composition value for one household. So, the number of rows for each combination depends on the values provided e.g. 18 rows of (town.a, singles), 8 rows of (town.b, singles etc.). However, I just can't seem to figure out how to do this expansion based on the values in each Town column.
I have a data.frame() that looks like this:
household.data <- data.frame(household.composition= c("Singles","Couples", "Families", "Single Parents", "Sharers"),
town.a =c(18,29,41,3,3),
town.b =c(8,37,48,9,3))
The values under the Town A and Town B columns represent the per cent makeup of each household composition within each town.
The goal is to get from this wide summary format to a long format which multiplies the value in the Household Composition column by the numeric value within the Town A and Town B columns. So each row would represent the household composition value for one household. For example:
Again, I know that there must be a way to do this using the spread/gather or pivot function in tidyR. However, I just can't seem to figure out how to do this expansion given that I would like the number of rows to correspond with the per cent value.