[R] Check if a character vector can be coerced to numeric?

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Mar 21 19:31:02 CET 2013


On 21/03/2013 18:20, Jonathan Greenberg wrote:
> Given an arbitrary set of character vectors:
>
> myvect1 <- c("abc","3","4")
> myvect2 <- c("2","3","4")
>
> I would like to develop a function that will convert any vectors that can
> be PROPERLY converted to a numeric (myvect2) into a numeric, but leaves
> character vectors which cannot be converted (myvect1) alone.  Is there any
> simple way to do this (e.g. some function that tests if a vector is
> coercible to a numeric before doing so)?
>
> --j

?type.convert

It does depend what you mean by 'properly'.  Can 
"123.456789012344567890123455" be converted 'properly'?  [See the NEWS 
for R-devel.]

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list