[Rd] silent recycling in logical indexing

Berry, Charles ccberry at ucsd.edu
Thu Jan 4 22:09:11 CET 2018



> On Jan 4, 2018, at 11:56 AM, Ben Bolker <bbolker at gmail.com> wrote:
> 
> 
>  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? 

It is convenient to use a single `TRUE' in programmatic manipulation of subscripts in the same manner as using an empty subscript interactively:

> mat<-diag(1:3)
> expr1 <- quote(mat[])
> expr1[[3]] <- TRUE
> expr1[[4]] <- 2
> eval(expr1)
[1] 0 2 0
> mat[,2]
[1] 0 2 0

HTH,

Chuck


More information about the R-devel mailing list