Re: [R] as.integer question

From: Peter Dalgaard <P.Dalgaard_at_biostat.ku.dk>
Date: Thu, 17 Jan 2008 18:54:35 +0100

Erin Hodgess wrote:
> Hi R People:
>
> I'm reading "Statistical Computing with R", by Maria Rizzo, and it's
> really good.
>
> Anyhow, I have a question about something in there.
>
>
>> u <- runif(5)
>> u
>>
> [1] 0.1177041 0.4271790 0.4601597 0.2204846 0.4051473
>
>> #in the book
>> sum(as.integer(u > 0.4))
>>
> [1] 3
>
>> #what I would do
>> sum(u > 0.4)
>>
> [1] 3
>
>
> Is one way better than the other, please?
>
> Thanks,
> Erin
>
>
I'd do what you'd do, but I suppose that the author felt that it was clearer to make the coercion explicit. In terms of speed, your version is likely faster (but what would you do with those extra 17 microseconds of life?).

-p

-- 
   O__  ---- Peter Dalgaard             Ă˜ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard_at_biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
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.
Received on Thu 17 Jan 2008 - 17:59:26 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 17 Jan 2008 - 18:30:07 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive