[R] Quick Question about R

Marc Schwartz marc_schwartz at comcast.net
Wed Jan 31 20:48:59 CET 2007


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



More information about the R-help mailing list