简体   繁体   中英

Frequency tables by ID in R

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:

在此处输入图像描述

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:

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM