From wdunlap at tibco.com  Thu Jan  4 21:50:04 2018
From: wdunlap at tibco.com (William Dunlap)
Date: Thu, 4 Jan 2018 12:50:04 -0800
Subject: [Rd] silent recycling in logical indexing
In-Reply-To: <6ba8e484-bc14-bcbb-1d8a-4871909e87f2@gmail.com>
References: <6ba8e484-bc14-bcbb-1d8a-4871909e87f2@gmail.com>
Message-ID: <CAF8bMcaxTpf6-5yN2dNZfzQfqwXK_ZEeSSn1JL2f8H78e_r7Zw@mail.gmail.com>

One use case is when you want to extract every third item, starting with
the second, of an arbitrary vector with
    x[c(FALSE, TRUE, FALSE)]
instead of
    x[seq_along(x) %% 3 == 2]

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, 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?  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:
>
> > x <- c(TRUE,TRUE,FALSE)
> > y <- c(TRUE,FALSE)
> > x[y]
> [1]  TRUE FALSE
>
> ## (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
> [1] TRUE TRUE TRUE
> 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 ...
>
>  Thoughts?
>
>    cheers
>      Ben Bolker
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

	[[alternative HTML version deleted]]



[Rd] silent recycling in logical indexing
William Dunlap