Reputation: 87
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
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