我需要在R中做一个小练习,我需要知道一个特定单词出现在字符串中的次数以及每个单词的位置。
我有这个:
string = 'a b a b c d a a g'
splitstring = strsplit(string, ' ')
sapply(gregexpr("a", splitstring, fixed= TRUE), function(x) sum(x>-1))
我的输出是:[1] 4
,所以我的字符串中有四个“ a”,现在我想知道它们的位置。
gregexpr
给您职位:
gregexpr("a", string, f=T)[[1]]
# [1] 1 5 13 15
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句