-1

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?

ponury-kostek
  • 7,824
  • 4
  • 23
  • 31
  • 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 Answers2

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,411
  • 2
  • 11
  • 21
0

tidyverse solution

library( tidyverse )
df %>% group_by( color ) %>% summarise( number = n() )
Wimpel
  • 26,031
  • 1
  • 20
  • 37