Jonathan Greenberg
2013-Mar-21 18:20 UTC
[R] Check if a character vector can be coerced to numeric?
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 -- Jonathan A. Greenberg, PhD Assistant Professor Global Environmental Analysis and Remote Sensing (GEARS) Laboratory Department of Geography and Geographic Information Science University of Illinois at Urbana-Champaign 607 South Mathews Avenue, MC 150 Urbana, IL 61801 Phone: 217-300-1924 http://www.geog.illinois.edu/~jgrn/ AIM: jgrn307, MSN: jgrn307@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]]
Prof Brian Ripley
2013-Mar-21 18:31 UTC
[R] Check if a character vector can be coerced to numeric?
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
Jeff Newmiller
2013-Mar-21 18:36 UTC
[R] Check if a character vector can be coerced to numeric?
Using capital letters does not improve clarity... it just offends people. Does read.table and friends not do this to your satisfaction already with as.is=TRUE? If not, shouldn't coercing it and checking for NA serve? --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. Jonathan Greenberg <jgrn at illinois.edu> 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