Home > Back-end >  Count repeated elements (variable length) in a vector with R
Count repeated elements (variable length) in a vector with R

Time:11-16

We know that a vector will contain repeated elements, and the pattern is

c("A","B","C","D")

But a subset of this pattern will be used, and it will always begin at A, and the order will be the same.

A simple example is

c("A","A","B","A","A","B","A","B","C","D")

And we can structure it in this way:

c("A",
"A","B",
"A",
"A","B",
"A","B","C","D")

I would like an output vector counting the length of the pattern:

c(1,2,1,2,4)

CodePudding user response:

This would would:

x1 <- c("A","A","B","A","A","B","A","B","C","D")
diff(c(which(x1 == "A"), length(x1) 1))

CodePudding user response:

I couldn't tell if you were interested in keeping the intermediate list of vectors. If not, @nicola's answer in the comments is the most elegant solution. If so, then I think adapting this answer and then taking the lengths may be useful:

inp <- c("A","A","B","A","A","B","A","B","C","D")

split.vec <- function(vec, sep = 0) {
    is.sep <- vec == sep
    split(vec, cumsum(is.sep))
}
out <- split.vec(inp, "A")
sapply(out, length)

# 1 2 3 4 5 
# 1 2 1 2 4 
  • Related