Hi R People: 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. Is one more efficient or is this just a matter of programming style, please? Thanks in advance, Sincerely, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
On Wed, 8 Oct 2008, Erin Hodgess wrote:> Hi R People: > > 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. > > > Is one more efficient or is this just a matter of programming style, please?What would be more efficient is primes <- integer(0) (as it looks like 'primes' concatenates integer vectors, at a quick glance). Use a function call c() to get NULL is not efficient, but all the differences here are tiny.> Thanks in advance, > Sincerely, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.com > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
> 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}}
On 08/10/2008 1:48 AM, Erin Hodgess wrote:> Hi R People: > > 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. > > > Is one more efficient or is this just a matter of programming style, please?It was purely a choice of style. By the way, there is an error in one of the programs coming soon after that: the mergesort example that starts on p. 68 doesn't handle odd-length input properly, because it uses "len / 2" in a number of places where it really needs "len %/% 2". So for example, when len is 3 we get behaviour like this: > x <- 1:3 > len <- 3 > x[1:(len/2)] [1] 1 > x[(len/2 + 1):len] [1] 2 (and the 3 got lost). Using the integer divide is fine: > x[1:(len %/% 2)] [1] 1 > x[(len %/% 2 + 1):len] [1] 2 3 Duncan Murdoch
On 8/10/2008, at 6:48 PM, Erin Hodgess wrote:> Hi R People: > > 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. > > > Is one more efficient or is this just a matter of programming > style, please?I think that using primes <- c() rather than primes <- NULL is a load of dingos' kidneys. ;-) [Hi John!!!] cheers, Rolf ###################################################################### Attention:\ This e-mail message is privileged and confid...{{dropped:9}}