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?
Using
table
:This would also work but is less eloquent than the plyr solution:
This should do the trick:
This requires package plyr.
A
data.table
approachdata.table
v >= 1.9.5 has a built inuniqueN
function nowHere is a benchmark of @David Arenburg's solution there as well as a recap of some solutions posted here (@mnel, @Sven Hohenstein, @Henrik):
Results:
Plot:
Session info:
In
dplyr
you may usen_distinct
to "count the number of unique values":