0

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.).

klaskowski
  • 11
  • 3
  • Try `library(reshape2);dM <- melt(have.this, id.var = c("Pond", "Size")); transform(dM[rep(1:nrow(dM), dM$value),], variable = gsub("Species|\\.count", "", variable))` – akrun Feb 20 '17 at 15:28
  • 1
    Perfect, thanks! Using rep in the transform bit seems to be the key that I couldn't quite figure out. Much appreciated. – klaskowski Feb 21 '17 at 10:18

0 Answers0