0

Background

First: I know this looks like several other similarly-titled posts, but I promise mine's different.

So I've got this table:

df <- data.frame(ID = c("a","a","a","b", "c","c","c","c","d","d","d","d"),
                 category = c(0,0,0,2,1,1,1,1,0,0,0,0),
                 stringsAsFactors=FALSE)

It's got some people (ID) and a categorical variable category that has three levels: 0, 1, and 2. As you can see, there are 2 IDs who have category 0, and 1 ID each for categories 1 and 2. Note that no IDs have "mixed" entries for category: regardless of how many rows, an ID will only have the 1 category.

The Problem

I'm trying to get a frequency table of category that tabulates on the basis of ID, and not just a straight count of category. In other words, I'm looking for something like this:

enter image description here

What I've tried

I've tried several things based on similar posts, but just can't get it right. For example, this:

df %>% group_by(category) %>% summarise(Freq=n())

But that's not quite it: it's getting me the groups I want (0,1,2) but still counting by individual instances of the categories:

enter image description here

logjammin
  • 1,121
  • 6
  • 21

1 Answers1

3

This should do it:

> table(unique(df)$category)
0 1 2 
2 1 1 

Or if you really want a data frame:

> as.data.frame(table(unique(df)$category))
  Var1 Freq
1    0    2
2    1    1
3    2    1
Necklondon
  • 928
  • 4
  • 12