Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Count the number of instances of a string of text in an entire dataframe

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading