Hello, Is there a way to convert a character to a number with out getting a warning? I have a vector that has both numbers and letters in it and I need to convert it to only numbers. At the moment I'm using as.numeric but it is generating a warning when it converts a letter. Is there another function out there that will do what I need or is there a way to turn off the warnings as I don't want the warning to be displayed to the end user? Konrad Hammel Engineer Prilink LTD konrad.hammel@prilink.com 905.305.1096 [[alternative HTML version deleted]]
suppressWarnings(a <- as.numeric(c(1, 2, pi, "a", 9, "z"))) b On Jan 31, 2007, at 2:35 PM, Konrad wrote:> Hello, > Is there a way to convert a character to a number with out getting > a warning? I have a vector that has both numbers and letters in it > and I need to convert it to only numbers. At the moment I'm using > as.numeric but it is generating a warning when it converts a > letter. Is there another function out there that will do what I > need or is there a way to turn off the warnings as I don't want the > warning to be displayed to the end user? > > Konrad Hammel > Engineer > Prilink LTD > konrad.hammel at prilink.com > 905.305.1096 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.
Konrad wrote:> Hello, > Is there a way to convert a character to a number with out getting a warning? I have a vector that has both numbers and letters in it and I need to convert it to only numbers. At the moment I'm using as.numeric but it is generating a warning when it converts a letter. Is there another function out there that will do what I need or is there a way to turn off the warnings as I don't want the warning to be displayed to the end user??options options(warn = -1) as.numeric(c("1","A","2")) [1] 1 NA 2 options(warn = 0) # return to default> Konrad Hammel > Engineer > Prilink LTD > konrad.hammel at prilink.com > 905.305.1096 > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
On Wed, 2007-01-31 at 14:35 -0500, Konrad wrote:> Hello, > Is there a way to convert a character to a number with out getting a > warning? I have a vector that has both numbers and letters in it and > I need to convert it to only numbers. At the moment I'm using > as.numeric but it is generating a warning when it converts a letter. > Is there another function out there that will do what I need or is > there a way to turn off the warnings as I don't want the warning to be > displayed to the end user?If you provide an example of your data, we can offer more definitive assistance, especially if the formats are in a regular pattern. However, you might want to look at: ?gsub (and perhaps ?regex) ?strsplit ?substr ?strtrim One quick example:> V <- paste(sample(c(LETTERS, 0:9), 20), collapse = "")> V[1] "D7YL1N5U2H8QCS03RXMZ"> gsub("[A-Z]", "", V)[1] "7152803"> as.numeric(gsub("[A-Z]", "", V))[1] 7152803 HTH, Marc Schwartz
On 31-Jan-07 Konrad wrote:> Hello, > Is there a way to convert a character to a number with out getting a > warning? I have a vector that has both numbers and letters in it and I > need to convert it to only numbers. At the moment I'm using as.numeric > but it is generating a warning when it converts a letter. Is there > another function out there that will do what I need or is there a way > to turn off the warnings as I don't want the warning to be displayed to > the end user?Have a look at the entries "warn" and "warning.expression" under ?options In particular:> options()$warn[1] 0> x <- c(1,2,"C",4,"E",6,"G") > as.numeric(x)[1] 1 2 NA 4 NA 6 NA Warning message: NAs introduced by coercion> old.warn <- options()$warn > options(warn = -1) > as.numeric(x)[1] 1 2 NA 4 NA 6 NA> options(warn = old.warn) > as.numeric(x)[1] 1 2 NA 4 NA 6 NA Warning message: NAs introduced by coercion>Note that options(warn = -1) turns off all warning messages everywhere, so you may want to bracket your calls to as.numeric() in the way shown above, to avoid intefering with other cases where you may want the user to see the warning. Hoping this helps, Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <ted.harding at nessie.mcc.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 31-Jan-07 Time: 19:59:33 ------------------------------ XFMail ------------------------------
On 31-Jan-07 Benilton Carvalho wrote:> suppressWarnings(a <- as.numeric(c(1, 2, pi, "a", 9, "z")))Of course! Much better! Ted. -------------------------------------------------------------------- E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk> Fax-to-email: +44 (0)870 094 0861 Date: 31-Jan-07 Time: 20:02:25 ------------------------------ XFMail ------------------------------