2016-03-23 34 views
2

我有一個約數的問題有多少串grep的多個模式,並指望有多少中的串R

例如,匹配模式 我有一個數據幀的'

index string 
1  'I have first and second' 
2  'I have first' 
3  'I have second and first and third' 

和所謂toMatch

匹配模式矢量
toMatch <- c('first', 'second', 'third') 

最後的結果我想是這樣的:

string        count 
'I have first and second'   2 
'I have first'      1 
'I have second and first and third' 3 

現在我只能用

grepl(paste(toMatch, collapse = "|"), s$string) 

將返回我這串匹配任何元素的toMatch,但我怎麼能知道有多少元素相匹配?

任何幫助將不勝感激!提前致謝!

回答

2
data.frame(string=s$string, count=rowSums(sapply(toMatch, function(x) grepl(x, s$string)))) 
+0

謝謝!有用! ^^ –

1

另一種可能性,即可能會更快:

您的數據:

dat <- read.table(text="index string 
1  'I have first and second' 
2  'I have first' 
3  'I have second and first and third'", header=TRUE) 

toMatch <- c('first', 'second', 'third') 

的方法:

library(stringi) 
dat$count <- stri_count_regex(dat$string, paste(toMatch, collapse="|")) 
dat 

## index       string count 
## 1  1   I have first and second  2 
## 2  2      I have first  1 
## 3  3 I have second and first and third  3