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?
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With