Julia Liu
Julia Liu

Reputation: 1

Counting Frequency in a column with certain words

New to R, and need some help! I have a large dataset where it is difficult to shift (I had to collapse it down to 10,000 rows).

I essentially need to grab the frequency for the color red and blue within column, but the values can range from "RED", "RED.", "Red", "red.", "rEd", etc. There are multiple ways that it is written that it is difficult to scroll down and input each one.

I have tried this:

table(DATAFRAME$COLOR=="RED")
table(DATAFRAME$COLOR=="RED.")
table(DATAFRAME$COLOR=="Red")
table(DATAFRAME$COLOR=="Red.")
table(DATAFRAME$COLOR=="rEd/Blue")

Is there an easier way to just grab any values that has red/blue?

Upvotes: 0

Views: 36

Answers (1)

George
George

Reputation: 903

This should be able to do what you need, although without a MWE I can't be sure

df<-data.frame(color =c("red", "Red", "Red", "RED", "rED", "blue", 'yelow'), var1=1:7)

sum(grepl("red", df$color, ignore.case = T))

Upvotes: 1

Related Questions