[Rd] subsetting by name is very slow when subscript contains a lot of "invalid" names

Hervé Pagès hpages at fhcrc.org
Wed May 8 22:53:57 CEST 2013


Note sure why but subsetting by name is *very* slow when the character
vector used as subscript contains a lot of "invalid" names:

   x <- c(A=10L, B=20L, C=30L)
   subscript <- c(LETTERS[1:3], sprintf("ID%05d", 1:150000))

   > system.time(y1 <- x[subscript])
      user  system elapsed
   111.991   0.000 112.230

Since subsetting by name is basically equivalent to

   i <- match(subscript, names(x))

it's quite surprising that the former is more than 10 thousand times
slower than the latter:

   > system.time({i <- match(subscript, names(x)); y2 <- x[i]})
      user  system elapsed
     0.008   0.000   0.007

   > identical(y2, y1)
   [1] TRUE


PS: This issue was already reported here
in 2010, and with a proposed fix by Martin Morgan.

 > sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-unknown-linux-gnu (64-bit)

  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
  [7] LC_PAPER=C                 LC_NAME=C
  [9] LC_ADDRESS=C               LC_TELEPHONE=C

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods
[8] base

other attached packages:
[1] GenomicRanges_1.13.8 IRanges_1.19.3       BiocGenerics_0.7.2

loaded via a namespace (and not attached):
[1] stats4_3.0.0 tools_3.0.0

Hervé Pagès

Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M1-B514
P.O. Box 19024
Seattle, WA 98109-1024

E-mail: hpages at fhcrc.org
Phone:  (206) 667-5791
Fax:    (206) 667-1319

More information about the R-devel mailing list