Andrew Yee
2010-Apr-09 23:22 UTC
[R] using as.numeric() without generating warning message
I'm interested in testing whether or not a character string is numeric or not as follows: is.na(as.numeric('3')) # returns F is.na(as.numeric('A')) # I'd like this to return T without issuing a warning about NAs introduced by coercion. I guess you could suppress the warning with options(warn=-1), but I was wondering if there's an alternative. Or perhaps an alternative that doesn't use as.numeric()? Thanks, Andrew [[alternative HTML version deleted]]
Bert Gunter
2010-Apr-10 00:01 UTC
[R] using as.numeric() without generating warning message
I suspect the following is way slower and clunkier, but it does give you another way: ?tryCatch ## as in> tryCatch(1+x,error=function(e) return("darnit"))[1] "darnit" Cheers, Bert Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andrew Yee Sent: Friday, April 09, 2010 4:23 PM To: r-help at stat.math.ethz.ch Subject: [R] using as.numeric() without generating warning message I'm interested in testing whether or not a character string is numeric or not as follows: is.na(as.numeric('3')) # returns F is.na(as.numeric('A')) # I'd like this to return T without issuing a warning about NAs introduced by coercion. I guess you could suppress the warning with options(warn=-1), but I was wondering if there's an alternative. Or perhaps an alternative that doesn't use as.numeric()? Thanks, Andrew [[alternative HTML version deleted]] ______________________________________________ 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.
Gabor Grothendieck
2010-Apr-10 00:04 UTC
[R] using as.numeric() without generating warning message
Try this: suppressWarnings(as.numeric("A")) On Fri, Apr 9, 2010 at 7:22 PM, Andrew Yee <yee at post.harvard.edu> wrote:> I'm interested in testing whether or not a character string is numeric or > not as follows: > > is.na(as.numeric('3')) # returns F > is.na(as.numeric('A')) # I'd like this to return T without issuing a warning > about NAs introduced by coercion. > > I guess you could suppress the warning with options(warn=-1), but I was > wondering if there's an alternative. ?Or perhaps an alternative that doesn't > use as.numeric()? > > Thanks, > Andrew > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Bert Gunter
2010-Apr-10 00:05 UTC
[R] using as.numeric() without generating warning message
Scratch my previous suggestion. Utter nonsense. Sigh... It's been a tough week. -- Bert Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Andrew Yee Sent: Friday, April 09, 2010 4:23 PM To: r-help at stat.math.ethz.ch Subject: [R] using as.numeric() without generating warning message I'm interested in testing whether or not a character string is numeric or not as follows: is.na(as.numeric('3')) # returns F is.na(as.numeric('A')) # I'd like this to return T without issuing a warning about NAs introduced by coercion. I guess you could suppress the warning with options(warn=-1), but I was wondering if there's an alternative. Or perhaps an alternative that doesn't use as.numeric()? Thanks, Andrew [[alternative HTML version deleted]] ______________________________________________ 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.