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 -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
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
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, > ErinThere is additional coercion overhead in the first approach, since as.integer() is called separately: set.seed(1) Vec <- sample(c(TRUE, FALSE), 1000000, replace = TRUE) > system.time(sum(Vec)) user system elapsed 0.004 0.000 0.025 > system.time(sum(as.integer(Vec))) user system elapsed 0.013 0.019 0.050 To paraphrase a financial quote: A microsecond here, a microsecond there and pretty soon you are talking about a serious amount of time... ;-) HTH, Marc Schwartz