Counting unique / distinct values by group in a data frame

Mehper C. Palavuzlar picture Mehper C. Palavuzlar · Oct 11, 2012 · Viewed 189.4k times · Source

Let's say I have the following data frame:

> myvec
    name order_no
1    Amy       12
2   Jack       14
3   Jack       16
4   Dave       11
5    Amy       12
6   Jack       16
7    Tom       19
8  Larry       22
9    Tom       19
10  Dave       11
11  Jack       17
12   Tom       20
13   Amy       23
14  Jack       16

I want to count the number of distinct order_no values for each name. It should produce the following result:

name    number_of_distinct_orders
Amy     2
Jack    3
Dave    1
Tom     2
Larry   1

How can I do that?

Answer

mnel picture mnel · Oct 16, 2012

A data.table approach

library(data.table)
DT <- data.table(myvec)

DT[, .(number_of_distinct_orders = length(unique(order_no))), by = name]

data.table v >= 1.9.5 has a built in uniqueN function now

DT[, .(number_of_distinct_orders = uniqueN(order_no)), by = name]