[R] sprintf + integer(0) problem

Henrique Dallazuanna wwwhsd at gmail.com
Wed Feb 24 19:17:27 CET 2010


You can try this:

cat(sprintf(ifelse(any(grepl(s, nn)), 'found %s in col %d\n', 'Column
%s not found'), s, match(s, nn)))

On Wed, Feb 24, 2010 at 3:06 PM, Esmail <esmail.js at gmail.com> wrote:
> 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
>
> ______________________________________________
> 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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O



More information about the R-help mailing list