From h@wickh@m @ending from gm@il@com  Fri Jun  8 23:15:20 2018
From: h@wickh@m @ending from gm@il@com (Hadley Wickham)
Date: Fri, 8 Jun 2018 14:15:20 -0700
Subject: [Rd] Subsetting the "ROW"s of an object
In-Reply-To: <A63CB03F-000C-4A80-B8F9-621A075F6F6D@ucsd.edu>
References: <CABdHhvF9R3Mpk9pMMy8wa=qXzGta9cXdb9BYKT4P69QRuU3rig@mail.gmail.com>
 <F528270A-70D5-457B-BE73-4562540CFA16@ucsd.edu>
 <CABdHhvFcNObMmNvs9BfDk6sgZspNFK2++a571RrwLz-2taF4oQ@mail.gmail.com>
 <83c9327a-d8f1-66da-c75f-13e67a0140e0@fredhutch.org>
 <C43ED250-AD89-4ECC-97B8-038231DB475D@ucsd.edu>
 <CABdHhvHDFyEBESAFaE99rm5_xgdG08ooCAFFkshCvY86y9RmCg@mail.gmail.com>
 <A15318D9-58CD-4514-AD9D-4B9F3FFC3A66@ucsd.edu>
 <CABdHhvEQqx0yJ_b2cwt5iO+azCLqCdMck_v0HDHT0v5FBqOd1g@mail.gmail.com>
 <A63CB03F-000C-4A80-B8F9-621A075F6F6D@ucsd.edu>
Message-ID: <CABdHhvGJm5CCObSQ+4VqP10TMM5b220GzWyZZJD+66xOOYPrtQ@mail.gmail.com>

On Fri, Jun 8, 2018 at 2:09 PM, Berry, Charles <ccberry at ucsd.edu> wrote:
>
>
>> On Jun 8, 2018, at 1:49 PM, Hadley Wickham <h.wickham at gmail.com> wrote:
>>
>> Hmmm, yes, there must be some special case in the C code to avoid
>> recycling a length-1 logical vector:
>
>
> Here is a version that (I think) handles Herve's issue of arrays having one or more 0 dimensions.
>
> subset_ROW <-
>     function(x,i)
> {
>     dims <- dim(x)
>     index_list <- which(dims[-1] != 0L) + 3
>     mc <- quote(x[i])
>     nd <- max(1L, length(dims))
>     mc[ index_list ] <- list(TRUE)
>     mc[[ nd + 3L ]] <- FALSE
>     names( mc )[ nd+3L ] <- "drop"
>     eval(mc)
> }
>
> Curiously enough the timing is *much* better for this implementation than for the first version I sent.
>
> Constructing a version of `mc' that looks like `x[i,,,,drop=FALSE]' can be done with `alist(a=)' in place of `list(TRUE)' in the earlier version but seems to slow things down noticeably. It requires almost twice (!!) as much time as the version above.

I think that's probably because alist() is a slow way to generate a
missing symbol:

bench::mark(
  alist(x = ),
  list(x = quote(expr = )),
  check = FALSE
)[1:5]
#> # A tibble: 2 x 5
#>   expression                    min     mean   median      max
#>   <chr>                    <bch:tm> <bch:tm> <bch:tm> <bch:tm>
#> 1 alist(x = )                 2.8?s   3.54?s   3.29?s   34.9?s
#> 2 list(x = quote(expr = ))    169ns 219.38ns    181ns   24.2?s

(note the units)

Hadley


-- 
http://hadley.nz



[Rd] Subsetting the "ROW"s of an object
Hadley Wickham