[R] Simple Lookup... why so slow

Dieter Menne dieter.menne at menne-biomed.de
Fri Aug 6 14:42:11 CEST 2004

Dear List,

At 32 degrees Celsius in the office, I was too lazy to figure out
the correct xapplytion for a simple lookup problem
and regressed to well-known c-style. Only to see my
computer hang forever doing 10000 indexed offset calculation.
Boiled down, the problem is shown below; needs a few milliseconds
in c. Looking at the timing results of n=2000 and n=4000,
this is not linear in time, so something I don't understand
must go on.

And, just as an aside: why is $-indexing so much faster (!)
than numeric indexing?


(all on Windows, latest R-Version)

# Generate Data set
StartDay = matrix(as.integer(runif(80)*20),nrow=4)
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=NA) # reserve output space
# Correct for days offset
ti= system.time(
  for (i in 1:n)
    PatDay$DayOff[i] = PatDay$Day[i]-StartDay[PatDay$Treat[i],PatDay$Pat[i]]
cat("$Style index",n,ti[3],"\n");
# n= 2000 3 seconds
# n= 4000 15 seconds

# I first believed using numeric indexes could be faster...
ti= system.time(
  for (i in 1:n)
    PatDay[i,4] = PatDay[i,1]-StartDay[PatDay[i,3],PatDay[i,2]]
cat("Numeric index", n,ti[3],"\n");
# n=2000 12 seconds
# n=4000 53 seconds

More information about the R-help mailing list