[R] Re: Simple Lookup... why so slow

Dieter Menne dieter.menne at menne-biomed.de
Fri Aug 6 16:06:23 CEST 2004


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




More information about the R-help mailing list