1

I have same trouble with the package dplyr. My data set has 3 varaibles; ID= number of individuum, Exp=number of Experiment, Zone= zone wihtin the test vessel (1 top 2 centre 3 bottom). I try to count the observation of the data which I grouped by ID and Exp and it works with the below equation.

    x<-data%>%
       group_by(ID,Exp)%>%
       count(Zone)

What the problem is, when one ID was not within Zone 1 than the new dataset will not display this, of course because there are no observation to count. What I try to do now is, to insert a "which" opperator to say the function, "if you do not count a observation within zone 1 (or 2 or 3) set the value 0". Has someone an idea how to fix my problem?

Thanks!

markus
  • 25,843
  • 5
  • 39
  • 58
S.Hartmann
  • 11
  • 1
  • 1
    please include the `data` object so that your issue can be reproduced. see here for more details: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dmca Dec 13 '18 at 17:19

1 Answers1

0

Without a reproducible example I can't be sure, but I believe you want to use tidyr::complete() to get the result you are describing. This example of mtcars should make it clear:

library(dplyr)
mtcars %>%
  count(cyl, carb) %>%
  tidyr::complete(cyl, carb, fill = list(n = 0))
dmca
  • 675
  • 1
  • 8
  • 18