I have a dataframe and I want to count the number of instances of a particular string of text.
For example in the below dataframe:
library(tidyverse)
df<-iris%>%
select(Species)%>%
distinct()%>%
mutate(Species2=Species)%>%
mutate(Species3=Species)
I want to count the number of times "setosa" occurs
Using length(grep("setosa", df$Species)) I can get the counts of setosa in a specific column but
how can I do this to the whole dataframe?
I tried length(grep("setosa", df)) which comes back as zero
Any suggestions?
>Solution :
Assuming you just want total count, rather than count by column, your code works if you convert the dataframe to a matrix first:
length(grep("setosa", as.matrix(df)))
which returns 3.
Note: unlike unlist() this also works when columns have different classes:
length(grep("OJ", as.matrix(ToothGrowth)))