If I have a categorical variable in my data frame (eg list of 100 colours) and their count is 5000+, how do I count the number of red colours, number of blue colours etc and store them?
Asked
Active
Viewed 62 times
-1
-
Do you want to count how many of each existing color appears? table(df$color) would do it, assuming your data frame `df` has a column `color`. Or do want to categorize the 100 colors into more general types? – Jon Spring Sep 14 '18 at 06:16
2 Answers
1
Easiest way to do it using Base R is to use table function
df_new<-as.data.frame(table(df$color,df$count))
This will give you count by various colors.
Hunaidkhan
- 1,390
- 2
- 10
- 20
0
tidyverse solution
library( tidyverse )
df %>% group_by( color ) %>% summarise( number = n() )
Wimpel
- 22,748
- 1
- 17
- 34