[R] sprintf + integer(0) problem
William Dunlap
wdunlap at tibco.com
Thu Feb 25 02:22:12 CET 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of Esmail
> Sent: Wednesday, February 24, 2010 10:07 AM
> To: r-help at r-project.org
> Subject: [R] sprintf + integer(0) problem
>
> 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?
Only call sprintf() if length(c) is positive (and
you may as well not call cat() in that case either):
if (length(c)>0) {
cat(sprintf('found %s in col %d\n', s, c))
}
It probably isn't worth the time, cpu or yours,
to omit the if statement for newer versions of R,
but you might put a comment in there that the limitation
in sprintf went away in 2.9 or so.
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
More information about the R-help
mailing list