Skip to content
Prev 5469 / 21312 Next

[Bioc-devel] Subsetting Lists by Lists

On 04/01/2014 02:43 PM, Michael Lawrence wrote:
The pseq_len() utility I sent previously solves your pluckHead()
problem:

   pluckHead <- function(x, n=6)
   {
     x[pseq_len(pmin(elementLengths(x), n))]
   }

or, using the non-exported utility IRanges:::fancy_mseq():

   pluckHead <- function(x, n=6)
   {
     x_eltlens <- unname(elementLengths(x))
     i_eltlens <- pmin(x_eltlens, n)
     i_skeleton <- PartitioningByEnd(cumsum(i_eltlens), names=names(x))
     unlisted_i <- IRanges:::fancy_mseq(i_eltlens)
     i <- relist(unlisted_i, i_skeleton)
     x[i]
   }

For pluckTail():

   pluckTail <- function(x, n=6)
   {
     x_eltlens <- unname(elementLengths(x))
     i_eltlens <- pmin(x_eltlens, n)
     i_skeleton <- PartitioningByEnd(cumsum(i_eltlens), names=names(x))
     offset <- x_eltlens - i_eltlens
     unlisted_i <- IRanges:::fancy_mseq(i_eltlens, offset)
     i <- relist(unlisted_i, i_skeleton)
     x[i]
   }

For both, 'n' can be of length > 1 and is recycled to the length of 'x'.
Negative values in 'n' are not supported but that should be easy to
add.

So I could add these 2 functions to IRanges, however, I'm not totally
convinced by the names. What about phead() and ptail() ("p" for
"parallel"), or vhead() and vtail() ("v" for "vectorized"), or mhead()
and mtail() (they're just fast equivalent to 'mapply(head, x, n)' and
'mapply(tail, x, n))', or...?

Thanks,
H.