[R] Re: Simple Lookup... why so slow
Gardar Johannesson
johannesson1 at llnl.gov
Fri Aug 6 17:17:34 CEST 2004
On other notes, try to leverage R's vector capabilities when
possible. Your DayOff variable can also be computed via
DayOff = PatDay$Day - StartDay[cbind(PatDay$Treat,PatDay$Pat)]
Gardar
At 04:06 PM 8/6/2004 +0200, Dieter Menne wrote:
>Ok, found it out. Things are really speedy when you first store result in a
>vector, and cbind the vector to the data frame later.
>
>Assuming that copying is involved, this would explain to me that my first
>approach was so much slower, but I don't understand why time goes up more
>than linearily with n.
>
>Dieter
>
>--
>
># Generate Data set
>StartDay = matrix(as.integer(runif(80)*20),nrow=4)
>n=8000
>PatDay = data.frame(Day = as.integer(runif(n)*20)+50,
> Pat= as.integer(runif(n)*20)+1,
> Treat = as.integer(runif(n)*4)+1
> )
>DayOff = rep(NA,n)
># Correct for days offset
>ti= system.time(
> for (i in 1:n)
># bad
># PatDay$DayOff[i] =
>PatDay$Day[i]-StartDay[PatDay$Treat[i],PatDay$Pat[i]]
># good
> DayOff[i] = PatDay$Day[i]-StartDay[PatDay$Treat[i],PatDay$Pat[i]]
> )
>PatDay$DayOff = DayOff
>cat("Separate Vector first",n,ti[3],"\n");
># n= 4000 0.43 seconds
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list