[R] question from Braun/Murdoch book
Duncan Murdoch
murdoch at stats.uwo.ca
Wed Oct 8 13:19:03 CEST 2008
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
More information about the R-help
mailing list