Re: [R] sprintf + integer(0) problem
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...@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@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 __ R-help@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.
Re: [R] sprintf + integer(0) problem
Henrique Dallazuanna wrote: 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))) Wow .. one can always learn something new .. that looks pretty complex, I have a bunch of print statements. I was hoping for a simpler solution so I guess for now I'll just comment out the sections that might cause problems under 2.8.0 (too bad, because it's useful to have some of this info displayed - which it does during development/testing, so it's ok). If anyone has a alternate solution, please let me know. In the meantime I'm going to try to use this code to determine if I should display the info or not: min = version[[minor]] maj = version[[major]] ver = paste(maj, '.',min, sep='') if (ver == 2.8.0) ... Cheers, Esmail -- View this message in context: http://n4.nabble.com/sprintf-integer-0-problem-tp1567829p1568357.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] sprintf + integer(0) problem
You can use tryCatch also: cat(tryCatch(sprintf('found %s in col %d\n', s, c), error = function(x)cat('Not Found\n'))) On Wed, Feb 24, 2010 at 9:49 PM, esmail esmail...@gmail.com wrote: Henrique Dallazuanna wrote: 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))) Wow .. one can always learn something new .. that looks pretty complex, I have a bunch of print statements. I was hoping for a simpler solution so I guess for now I'll just comment out the sections that might cause problems under 2.8.0 (too bad, because it's useful to have some of this info displayed - which it does during development/testing, so it's ok). If anyone has a alternate solution, please let me know. In the meantime I'm going to try to use this code to determine if I should display the info or not: min = version[[minor]] maj = version[[major]] ver = paste(maj, '.',min, sep='') if (ver == 2.8.0) ... Cheers, Esmail -- View this message in context: http://n4.nabble.com/sprintf-integer-0-problem-tp1567829p1568357.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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 __ R-help@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.
Re: [R] sprintf + integer(0) problem
-Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Esmail Sent: Wednesday, February 24, 2010 10:07 AM To: r-help@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@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. __ R-help@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.
Re: [R] sprintf + integer(0) problem
On 24-Feb-10 20:03, Henrique Dallazuanna wrote: You can use tryCatch also: cat(tryCatch(sprintf('found %s in col %d\n', s, c), error = function(x)cat('Not Found\n'))) Ah .. one more way .. thanks, I've saved it away in my set of R tricks! Best, Esmail __ R-help@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.