This is essentially a data reshaping question. I have a table that shows the counts of animals in different size classes that looks something like this:
have.this <- data.frame(Pond = c(1,1,1,2,2,2),
Size = c(1,2,3,1,2,3),
SpeciesA.count = c(2,6,1, 4,8,5),
SpeciesB.count = c(3,1,3,6,3,4))
However, now instead of having these different size classes, I want to have a row/observation for each individual. This is different from a standard wide-long reshaping as the length of the long format will completely depend on the count values. That is the length (number of observations) for Pond A will be 16 (2 individuals of Species A with size 1, 6 individuals of Species A with size 2 and so on), whereas the number of observations for Pond B will be 30.
want.this <- data.frame(Pond = c(rep(1,16), rep(2, 30)),
Species = c(rep("A", 9), rep("B", 17), rep("A", 7), rep("B", 13)),
Size = c(rep(1,2), rep(2,6), rep(3,1), rep(1,3), rep(2,1), rep(3,3),
rep(1,4), rep(2,8), rep(3,5), rep(1,6), rep(2,3), rep(3,4)))
I am familiar with 'gather' from tidyr and other reshaping commands, but this seems a bit different because the number of new rows depends on the particular values (in the 'SpeciesA.count' and 'SpeciesB.count' e.g.).