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,但我怎麼能知道有多少元素相匹配?
任何幫助將不勝感激!提前致謝!
謝謝!有用! ^^ –