Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting unique / distinct values by group in a data frame

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?

like image 818
Mehper C. Palavuzlar Avatar asked Oct 11 '12 13:10

Mehper C. Palavuzlar


People also ask

How do you count unique rows in a data frame?

Count Unique Rows in Pandas DataFrameUsing nunique() method, we can count unique rows in pandas. by default nunique() shows axis=0 that means rows but it can be changed to axis=1.


1 Answers

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] 
like image 87
mnel Avatar answered Sep 24 '22 05:09

mnel