I wish to count the number of unique values by grouping of a second variable, and then add the count to the existing data.frame as a new column. For example, if the existing
Here's a solution with the dplyr package - it has n_distinct() as a wrapper for length(unique()).
n_distinct()
length(unique())
df %>% group_by(color) %>% mutate(unique_types = n_distinct(type))