Calculates the number of sequences in a vector
•
Java
See English answer > How can I count runs in a sequence? 2
bin <- c(1,1,1)
I'll get 5 I haven't found any existing features that can do this. Does anyone have any good tips on how to write one? When sequences have different lengths, I don't know how to build counters
Solution
A run length coding function (RLE) is constructed When it helps calculate the run length of equal values in a vector, it returns the length with a value So use RLE (bin)
Use = = to compare the $value output with the expected value (1) and sum the results (because you get true or 1L when the value runs to 1):
sum( rle(bin)$values == 1 ) [1] 5
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码