g g
g g

Reputation: 87

package or function to count sequence lengths?

I was wondering if there is a package or generic function in R that counts sequence lengths. For instance, if I input a sequence

s1<-c('a','a','b','a','a','a','b','b')

The proposed function F(s1,'a') would return a vector: [2,3] and F(s1,'b') would return [1,2]

Upvotes: 2

Views: 993

Answers (1)

IRTFM
IRTFM

Reputation: 263451

Those madly typing people must have gone elsewhere:

 s1<- c('a','a','b','a','a','a','b','b')
 F1 <- function(s, el) {rle(s)$lengths[rle(s)$values==el] }
 F1(s1, "a")
#[1] 2 3
 F1(s1, "b")
#[1] 1 2

Upvotes: 4

Related Questions