Include empty factor levels in tally with tidyr and dplyr

By user4100013

a question as a learn dplyr and its ilk.

I am calculating a tally and a relative frequency of a factor conditioned on two other variables in a df. For instance:

library(dplyr)
library(tidyr)
set.seed(3457)
pct <- function(x) {x/sum(x)}
foo <- data.frame(x = rep(seq(1:3),20),
                  y = rep(rep(c("a","b"),each=3),10),
                  z = LETTERS[floor(runif(60, 1,5))])
bar <- foo %>%
group_by(x, y, z) %>%
tally %>%
mutate(freq = (n / sum(n)) * 100)
head(bar)

I’d like the output, bar, to include all the levels of foo$z. I.e., there are no cases of B here:

subset(bar, x==3 & y=="a")

How can I have bar tally the missing levels so I get:

subset(bar, x==3 & y=="a",select = n)

to return 3, 0, 4, 3 and select = freq to give 30, 0, 40, 30?

Many thanks.

Source: Stack Overflow

    

Share it with your friends!

    Fatal error: Uncaught Exception: 12: REST API is deprecated for versions v2.1 and higher (12) thrown in /home/content/19/9652219/html/wp-content/plugins/seo-facebook-comments/facebook/base_facebook.php on line 1273