From wdunlap at tibco.com  Thu Jan  4 23:07:54 2018
From: wdunlap at tibco.com (William Dunlap)
Date: Thu, 4 Jan 2018 14:07:54 -0800
Subject: [Rd] silent recycling in logical indexing
In-Reply-To: <CABghstSnaTTfxs+f0ENUeFJF4UO998_A4xdN7wFhZcmsuHAfog@mail.gmail.com>
References: <6ba8e484-bc14-bcbb-1d8a-4871909e87f2@gmail.com>
 <25B0F325-FD8D-4A43-B711-2329E4847AE9@ucsd.edu>
 <CABghstSnaTTfxs+f0ENUeFJF4UO998_A4xdN7wFhZcmsuHAfog@mail.gmail.com>
Message-ID: <CAF8bMcY2yXcphdwu=mmyV4A0XufnWtrcaRcAdt9vvB6_EVJC3Q@mail.gmail.com>

I have never used this construct.  However, part of my job is seeing how
well CRAN packages work in our reimplementation of the R language
and I am continually surprised by the inventiveness of package writers.

Bill Dunlap
TIBCO Software
wdunlap tibco.com

On Thu, Jan 4, 2018 at 1:44 PM, Ben Bolker <bbolker at gmail.com> wrote:

> Hmm.
>
> Chuck: I don't see how this example represents
> incomplete/incommensurate recycling. Doesn't TRUE replicate from
> length-1 to length-3 in this case (mat[c(TRUE,FALSE),2] would be an
> example of incomplete recycling)?
>
> William: clever, but maybe too clever unless you really need the
> speed? (The clever way is 8 times faster in the following case ...)
>
> x <- rep(1,1e6)
> rbenchmark::benchmark(x[c(FALSE,TRUE,FALSE)],x[seq_along(x) %% 3 == 2])
>
> On the other hand, it takes 0.025 vs 0.003 seconds per iteration ...
> fortunes::fortune("7ms")
>
>
> On Thu, Jan 4, 2018 at 4:09 PM, Berry, Charles <ccberry at ucsd.edu> wrote:
> >
> >
> >> 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
>

	[[alternative HTML version deleted]]



[Rd] silent recycling in logical indexing
William Dunlap