Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split an rle object

Tags:

r

Suppose I have the following rle object:

r = rle(c(rep("M",28),rep("N",4265),rep("M",16),rep("S",2),rep("N",400),rep("M",10)));

And I want to break it down to the following vector of strings:

a = c("28M","4265N","16M2S","400N","10M");

Meaning I separate "N" values and non "N" values and their corresponding lengths into separate elements in a vector.

Please note that all the non Ns are paste togetherm that is why the result has "16M2S", and not "16M" "2S" separated.

What would be the most efficient way to do this?

like image 657
user1701545 Avatar asked Feb 27 '26 14:02

user1701545


1 Answers

This works and should be decent speedwise:

l <- r$lengths
v <- r$values
s <- paste0(l, v)
n <- v == "N"
i <- cumsum(c(TRUE, head(n, -1) != tail(n, -1)))

unname(vapply(split(s, i), paste, character(1), collapse = ""))
# [1] "28M"   "4265N" "16M2S" "400N"  "10M"  
like image 181
flodel Avatar answered Mar 01 '26 04:03

flodel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!