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
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
> -----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. >