I am trying with dplyr functions group_by() and summarize(), count() but I can not figure out how to do :
How many of each color is assigned to each id? ie : id = 2 has 1 b and 1 r. id 3 has only 1 r ,...
df = data.frame(color = c("b","r","r","g","y","y","r"), id = c(2,3,2,6,4,4,7))
df %>%
group_by(id) %>%
summarize(count = n_distinct(color))
Is there any explicit way to do this? I would think of maybe creating dummies but my real dataset is very large and then I was wondering is there is a neat, pretty style of coding for this. Thanks for any suggestion.
output:
color id_2 id_3 id_4 id_6 id_7
1 b 1 0 0 0 0
2 g 0 0 0 1 0
3 r 1 1 0 0 1
4 y 0 0 2 0 0