Skip to content
Prev 31966 / 63424 Next

sprintf("%d", integer(0)) aborts

I was prompted to suggest the change by a note from Jim Holtman
in yesterday's R-help:

   > system.time({
   +     x <- sample(50000)  # test data
   +     x[sample(50000,10000)] <- 'asdfasdf'  # characters strings
   +     which.num <- grep("^[ 0-9]+$", x)  # find numbers
   +     # convert to leading 0
   +     x[which.num] <- sprintf("%018.0f", as.numeric(x[which.num]))
   +     x[-which.num] <- toupper(x[-which.num])
   + })

This code failed when I converted it to a function to run
through sapply because then which.num was often integer(0).
When used in production it would probably work for a long time
before seeing a sample in which which.num was integer(0).
(Of course, it would then silently mess up on the next line,
x[-which.num]<-...)
Those were my thoughts as well.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com