# How to calculate the number of occurrence of a given character in each row of a column of strings?

114

I have a data.frame in which certain variables contain a text string. I wish to count the number of occurrences of a given character in each individual string.

Example:

``````q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"))
``````

I wish to create a new column for q.data with the number of occurence of "a" in string (ie. c(2,1,0)).

The only convoluted approach I have managed is:

``````string.counter<-function(strings, pattern){
counts<-NULL
for(i in 1:length(strings)){
counts[i]<-length(attr(gregexpr(pattern,strings[i])[[1]], "match.length")[attr(gregexpr(pattern,strings[i])[[1]], "match.length")>0])
}
return(counts)
}

string.counter(strings=q.data\$string, pattern="a")

number     string number.of.a
1      1 greatgreat           2
2      2      magic           1
3      3        not           0
``````

This question is tagged with `regex` `r` `dataframe`

155

The stringr package provides the `str_count` function which seems to do what you're interested in

``````# Load your example data
q.data<-data.frame(number=1:3, string=c("greatgreat", "magic", "not"), stringsAsFactors = F)
library(stringr)

# Count the number of 'a's in each element of string
q.data\$number.of.a <- str_count(q.data\$string, "a")
q.data
#  number     string number.of.a
#1      1 greatgreat           2
#2      2      magic           1
#3      3        not           0
``````

``````x <- q.data\$string