Counting unique / distinct values by group in a data frame
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]
In dplyr
you may use n_distinct
to "count the number of unique values":
library(dplyr)
myvec %>%
group_by(name) %>%
summarise(n_distinct(order_no))