这个简单的base r
单行代码先使用 strsplit,然后使用 grepl,并且相当健壮,但如果必须将匹配计数jjjjjj
为 3 个jj
. 使这成为可能的模式匹配来自@JoshOBriens 出色的问答:
sum( grepl( "jj" , unlist(strsplit( x , "(?<=.)(?=jj)" , perl = TRUE) ) ) )
# Examples....
f<- function(x){
sum( grepl( "jj" , unlist(strsplit( x , "(?<=.)(?=jj)" , perl = TRUE) ) ) )
}
#3 matches here
xOP <- c("ajjss","acdjfkj","auyjyjjksjj")
f(xOP)
# [1] 3
#4 here
x1 <- c("ajjss","acdjfkj", "jj" , "auyjyjjksjj")
f(x1)
# [1] 4
#8 here
x2 <- c("jjbjj" , "ajjss","acdjfkj", "jj" , "auyjyjjksjj" , "jjbjj")
f(x2)
# [1] 8
#Doesn't work yet with multiple jjjj matches. We want this to also be 8
x3 <- c("jjjj" , "ajjss","acdjfkj", "jj" , "auyjyjjksjj" , "jjbjj")
f(x3)
# [1] 7