[R] sprintf + integer(0) problem

Esmail esmail.js at gmail.com
Wed Feb 24 19:06:56 CET 2010


Hello all,

I am stuck with R v2.8.0 under Linux for the time being and I am
running into a small problem that doesn't exist under 2.9.x and 2.10.x
with sprintf.

If I have the following code segment to help me determine the column
number for a specific column header/label:

  nn = names(Dataset)
  s = "Group"
  c = which(nn==s)

  cat(sprintf('found %s in col %d\n', s, c))


If the string s is found as a column header, sprintf works fine.

However if the string isn't found, c contains "integer(0)" which then
causes a program abort with the following message under 2.8.0

   "Error in sprintf("found %s in col %d\n", s, c) : zero-length argument"

Is there an easy work around? I tried using %s (in place of %d) hoping
it would just print "integer(0)" - which is better than a crash - but
that didn't work.

I am developing and testing my code under 2.9 and 2.10 but then
transferring it to a faster system that unfortunately still uses 2.8.0
.. I'd rather not have to keep modifying the source each time I upload it.

I'm hoping someone has an easy fix.

Thanks
Esmail

PS: in v 2.10.1 no output at all is generated when the string isn't found



More information about the R-help mailing list