[R] Vectors out of lists?

Bert Gunter gunter.berton at gene.com
Wed Nov 17 19:40:48 CET 2010


Eduardo:

On Wed, Nov 17, 2010 at 9:53 AM, Eduardo de Oliveira Horta
<eduardo.oliveirahorta at gmail.com> wrote:
> Thanks.
>
> I wanted to avoid loops (even inside functions) because I thought they were
> much slower than vector operations, and I really need an efficient code (not
> an exercise!).

This is a common miconception. What is true is that (internally)
vectorized functions -- which are loops in the **compiled C or Fortran
code -- are much faster than interpreted (at the R level) loops. So,
for example:

> system.time (y <- sin(seq_len(1e6)))
   user  system elapsed
   0.14    0.00    0.14
> y <- vector(length=1e6)
> system.time (for(i in seq_len(1e6))y[i] <- sin(i))
   user  system elapsed
   4.84    0.02    4.88

But Reduce(), Vectorize, and anything in the apply family **are**
(disguised) loops at the interpreted level (albeit done efficiently).
They have to be, since they are designed to work with arbitrary user
provided functions. So, continuing the example above, caclulating the
signs with sapply is no more efficient than using the explicit loop:

> system.time(sapply(seq_len(1e6),sin))
   user  system elapsed
   4.54    0.05    4.59


R also permits direct recursive implementation of loops; but because
of the heavy internal bookkeeping involved, this tends to be still
more inefficient.

Finally, IMO Bill Dunlap's pronouncements on R matters should always
be viewed as authoritative and generally also recommended R
programming practice. Therefore anything I say that conflicts with his
comments is wrong.

Cheers,
Bert


Bert Gunter
Genentech Nonclinical statistics



More information about the R-help mailing list