There are four observations in group a and three in group b:
d %>%count(g) -> countscounts
I didn’t know about this until fairly recently. Until then, I thought you had to do this:
d %>%group_by(g) %>%summarize(count=n())
which works, but is a lot more typing.
Going the other way
The other day, I had the opposite problem. I had a table of frequencies, and I wanted to get it back to one row per observation (I was fitting a model in Stan, and I didn’t know how to deal with frequencies). I had no idea how you might do that (without something ugly like loops), and I was almost embarrassed to stumble upon this:
counts %>%uncount(n)
My situation was a bit less trivial than that. I had disease category counts of coal miners with different exposures to coal dust:
So I wanted to fit an ordered logistic regression in Stan, predicting disease category from exposure, but I didn’t know how to handle the frequencies. If I had one row per miner, I thought…
… and so I do. (I scrolled down to check, and eventually got past the 98 miners with 5.8 years of exposure and no disease).
From there, you can use this to fit the model, though I would rather have weakly informative priors for their beta and c. c is tricky, since it is ordered, but I used the idea here (near the bottom) and it worked smoothly.