Reputation: 31968
Let us say I have a Rle like so, of length 10:
b = rle(c("H", "T", "T", "H", "H", "H", "H", "H", "T", "H"))
How can I get the length of this object without using inverse.rle
?
length(inverse.rle(b))
# 10
I have some sparse Rles representing chromosomes and they can have a length of hundreds of millions so I would rather not use inverse.
akruns answer does not work on my data:
> a
$ mydata
numeric-Rle of length 57442693 with 12471 runs
Lengths: 2709826 100 31062 100 ... 2 232 100 47
Values : 0 1 0 1 ... 1 0 1 0
> a$lengths
NULL
Upvotes: 3
Views: 322
Reputation: 31968
For the S4vectors
class Rle
you need to use the method runLength
, it has no accessor $length
.
In my example above, this becomes runLength(a[[1]])
.
Upvotes: 2
Reputation: 545923
Regarding your edit: akrun’s answer still generally works, you just need to adapt it slightly to your Rle
data type:
I don’t know what function you’re using here but in case this is anything like the Bioconductor Rle
S4 class, the solution would be
sum(s@lengths)
That is, instead of the nested name lengths
you have to use the S4 slot name lengths
. More generally, you can find out which solution works by inspecting:
names(obj)
.slotNames(obj)
.Upvotes: 2
Reputation: 887571
As the rle
is a list
object with lengths
and values
as elements of the list
, we can extract the lengths
and sum
sum(b$lengths)
#[1] 10
Upvotes: 6