Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count Pattern Matching in R

How would one efficiently count the number of instances of one character string which occur within another character string?

Below is my code to date. It successfully identifies if any instance of the one string occurs in the other string. However, I do not know how to extend it from a TRUE/FALSE relationship to a counting relationship.

x <- ("Hello my name is Christopher. Some people call me Chris")
y <- ("Chris is an interesting person to be around")
z <- ("Because he plays sports and likes statistics")

lll <- tolower(list(x,y,z))
dict <- tolower(c("Chris", "Hell"))

mmm <- matrix(nrow=length(lll), ncol=length(dict), NA)

for (i in 1:length(lll)) {
for (j in 1:length(dict)) {
    mmm[i,j] <- sum(grepl(dict[j],lll[i]))
}
}
mmm

It yields:

       [,1] [,2]
 [1,]    1    1
 [2,]    1    0
 [3,]    0    0

Since the lower-case string "chris" appears twice in the lll[1] I would like mmm[1,1] to be 2 instead of 1.

Real example is much higher dimension...so would love if code could be vectorized instead of using my brute force for loops.

like image 685
Chris Avatar asked Oct 29 '13 19:10

Chris


2 Answers

Two quick tips:

  1. avoid the dual for-loop, you dont need it ;)
  2. use the stringr package

library(stringr)

dict <- setNames(nm=dict)  # simply for neatness
lapply(dict, str_count, string=lll)
# $chris
# [1] 2 1 0
#
# $hell
# [1] 1 0 0

Or as a matrix:

#  sapply(dict, str_count, string=lll)
#      chris hell
# [1,]     2    1
# [2,]     1    0
# [3,]     0    0
like image 92
Ricardo Saporta Avatar answered Sep 25 '22 02:09

Ricardo Saporta


You can also do something like this:

count.matches <- function(pat, vec) sapply(regmatches(vec, gregexpr(pat, vec)), length)
mapply(count.matches, c('chris', 'hell'), list(lll))
#      chris hell
# [1,]     2    1
# [2,]     1    0
# [3,]     0    0
like image 34
Matthew Plourde Avatar answered Sep 24 '22 02:09

Matthew Plourde