Mehper C. Palavuzlar Mehper C. Palavuzlar - 2 months ago 9
R Question

Counting distinct values in a data frame in R

Let's say I have the following data frame in R:

> 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 R to count 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

This should do the trick:

ddply(myvec,~name,summarise,number_of_distinct_orders=length(unique(order_no)))

This requires package plyr.

Comments