[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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]] __ R-help@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.
Re: [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, rip...@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, UKFax: +44 1865 272595 __ R-help@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.
Re: [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 NewmillerThe . . Go Live... DCN:jdnew...@dcn.davis.ca.usBasics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/BatteriesO.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --- Sent from my phone. Please excuse my brevity. Jonathan Greenberg j...@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 __ R-help@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.
Re: [R] Check if a character vector can be coerced to numeric?
Yep, type.convert was exactly what I was looking for (with as.is=TRUE). Thanks! On Thu, Mar 21, 2013 at 1:31 PM, Prof Brian Ripley rip...@stats.ox.ac.ukwrote: 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, rip...@stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~**ripley/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, UKFax: +44 1865 272595 __** R-help@r-project.org mailing list https://stat.ethz.ch/mailman/**listinfo/r-helphttps://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/** posting-guide.html http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- 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: jgrn...@hotmail.com, Gchat: jgrn307, Skype: jgrn3007 [[alternative HTML version deleted]] __ R-help@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.