Here’s a solution with the dplyr package – it has n_distinct()
as a wrapper for length(unique())
.
df %>%
group_by(color) %>%
mutate(unique_types = n_distinct(type))
More Related Contents:
- Count number of rows per group and add result to original data frame
- Count number of occurences for each unique value
- Count unique values for every column
- How can I count the number of NAs per group?
- R transpose and group by
- Aggregate / summarize multiple variables per group (e.g. sum, mean)
- Faster ways to calculate frequencies and cast from long to wide
- Aggregating by unique identifier and concatenating related values into a string [duplicate]
- Extract row corresponding to minimum value of a variable by group
- Group by multiple columns and sum other multiple columns
- How can I remove all duplicates so that NONE are left in a data frame?
- Select the top N values by group
- How to assign a unique ID number to each group of identical values in a column [duplicate]
- Plot multiple lines (data series) each with unique color in R
- Aggregate multiple columns at once [duplicate]
- Aggregate a dataframe on a given column and display another column
- Aggregate a data frame based on unordered pairs of columns
- Compute mean and standard deviation by group for multiple variables in a data.frame
- aggregate methods treat missing values (NA) differently
- data.table with two string columns of set elements, extract unique rows with each row unsorted
- How to count the frequency of a string for each row in R
- Group integer vector into consecutive runs
- unique() for more than one variable
- Find the most frequent value by row
- Returning first row of group
- Count number of columns by a condition (>) for each row
- aggregate() puts multiple output columns in a matrix instead
- R group by aggregate
- R use ddply or aggregate
- Aggregate by week in R