[R] question from Braun/Murdoch book

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Wed Oct 8 11:52:01 CEST 2008


> I am looking at the Braun/Murdoch book, " A First Course in
> Statistical Programming in R", and I have a question about a function
> there.  It's on page 52, Example 4.5; the sieve of Erastosthenes.
> 
> There is a line:
> primes <- c()
> 
> Is there a difference between using that and
> primes <- NULL
> please?
> 
> When you put in primes <- c(), primes comes back as NULL.

They return the same thing
identical(c(), NULL)    #TRUE

> Is one more efficient or is this just a matter of programming style, 
please?
system.time(for(i in 1:1000000) c())
#   user  system elapsed 
#   0.63    0.02    0.64 

system.time(for(i in 1:1000000) NULL)
#   user  system elapsed 
#   0.28    0.00    0.28

Using NULL appears to be quicker on my machine, but given that you can do 
a million of these assignments in a fraction of a second, it makes no 
practical difference.  NULL is perhaps more intuitive than c() for 
demonstrating that the variable is empty.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list