简体   繁体   English

如何在R中的字符串中查找重复元素(子字符串)

[英]How to find repetitive elements(substring) in a string in R

a <- "ABDBBBLKDLKFFABDBOKKKMXKMABDBLPDLABDBKMKNABDBLKMXLSKMABDBOKOLKABDB"

如何查找字符串中“ ABDB”重复多少次?

stringi can do this very easily. stringi可以很容易地做到这一点。

library(stringi)
stri_count_fixed(a, "ABDB")
# [1] 7

Here is a solution that neither requires a loop over the string nor an external package: 这是一个既不需要循环字符串也不需要外部包的解决方案:

length(unlist(strsplit(paste0(a, "#"), "ABDB"))) - 1
#[1] 7

In this line of code, "#" is an auxiliary delimiter that is temporarily attached at the end of the string to make sure that occurrences of the pattern at the end are accounted for correctly. 在此代码行中, "#"是辅助定界符,该辅助定界符临时附加在字符串的末尾,以确保正确解决了末尾出现的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM