Count the maximum of consecutive letters in a string

那年仲夏 提交于 2019-12-10 15:16:21

问题


I have this vector:

vector <- c("XXXX-X-X", "---X-X-X", "--X---XX", "--X-X--X", "-X---XX-", "-X--X--X", "X-----XX", "X----X-X", "X---XX--", "XX--X---", "---X-XXX", "--X-XX-X")

I want to detect the maximum of consecutive times that appears X. So, my expected vector would be:

4, 1, 2, 1,2, 1, 2, 1, 2, 2, 3, 2

回答1:


In base R, we can split each vector into separate characters and then using rle find the max consecutive length for "X".

sapply(strsplit(vector, ""), function(x) {
   inds = rle(x)
   max(inds$lengths[inds$values == "X"])
})

#[1] 4 1 2 1 2 1 2 1 2 2 3 2



回答2:


Here is a slightly different approach. We can split each term in the input vector on any number of dashes. Then, find the substring with the greatest length.

sapply(vector, function(x) {
    max(nchar(unlist(strsplit(x, "-+"))))
})

XXXX-X-X ---X-X-X --X---XX --X-X--X -X---XX- -X--X--X X-----XX X----X-X 
       4        1        2        1        2        1        2        1 
X---XX-- XX--X--- ---X-XXX --X-XX-X 
       2        2        3        2 

I suspect that X really just represents any non dash character, so we don't need to explicitly check for it. If you do really only want to count X, then we can try removing all non X characters before we count:

sapply(vector, function(x) {
    max(nchar(gsub("[^X]", "", unlist(strsplit(x, "-+")))))
})



回答3:


Use strapply in gsubfn to extract out the X... substrings applying nchar to each to count its number of character producing a list of vectors of lengths. sapply the max function each such vector.

library(gsubfn)

sapply(strapply(vector, "X+", nchar), max)
## [1] 4 1 2 1 2 1 2 1 2 2 3 2



回答4:


Here are a couple of tidyverse alternatives:

map_dbl(vector, ~sum(str_detect(., strrep("X", 1:8))))
# [1] 4 1 2 1 2 1 2 1 2 2 3 2
map_dbl(strsplit(vector,"-"), ~max(nchar(.)))
# [1] 4 1 2 1 2 1 2 1 2 2 3 2


来源:https://stackoverflow.com/questions/53521119/count-the-maximum-of-consecutive-letters-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!