[Rd] silent recycling in logical indexing

Ben Bolker bbolker at gmail.com
Thu Jan 4 20:56:54 CET 2018

  Sorry if this has been covered here somewhere in the past, but ...

  Does anyone know why logical vectors are *silently* recycled, even
when they are incommensurate lengths, when doing logical indexing?  This
is as documented:

  For ‘[’-indexing only: ‘i’, ‘j’, ‘...’ can be logical
          vectors, indicating elements/slices to select.  Such vectors
          are recycled if necessary to match the corresponding extent.

but IMO weird:

> y <- c(TRUE,FALSE)
> x[y]

## (TRUE, FALSE) gets recycled to (TRUE,FALSE,TRUE) and selects
##  the first and third elements

If we do logical operations instead we do get a warning:

> x | y
Warning message:
In x | y : longer object length is not a multiple of shorter object length

  Is it just too expensive to test for incomplete recycling when doing
subsetting, or is there a sensible use case for incomplete recycling?

  Ll. 546ff of main/src/subscript.c suggest that there is a place in the
code where we already know if incomplete recycling has happened ...


     Ben Bolker

More information about the R-devel mailing list