ID= c('A', 'A', 'A', 'B', 'B', 'B')
color=c('white', 'green', 'orange', 'white', 'green', 'green')
d = data.frame (ID, color)
My desired result is
unique_colors=c(3,3,3,2,2,2)
d = data.frame (ID, color, unique_colors)
or more clear in a new dataframe c
ID= c('A','B')
unique_colors=c(3,2)
c = data.frame (ID,unique_colors)
I've tried different combinations of aggregate
and ave
as well as by
and with
and I suppose it is a combination of those functions.
The solution would include:
length(unique(d$color))
to calculate the number of unique elements.