[R] How to convert Charagter variables into numeric

Marc Schwartz marc_schwartz at comcast.net
Fri Feb 6 15:07:10 CET 2009

There is an issue that is being overlooked here.

The read.table() family of functions use type.convert() by default
internally to determine the resultant data types of the columns going
into the data frame from the source text file. Everything read in by
read.table() starts as character and then type.convert() (unless
overridden by 'colClasses') does its job.

If a column that "should be" numeric is being converted to a factor,
then there is a data integrity problem in that column. In other words,
there are non-numeric characters in that column, preventing the column
from being converted to numeric.

For example:

# All 'numeric' source data
> str(type.convert(as.character(1:5)))
 int [1:5] 1 2 3 4 5

# a non-numeric character in the source vector
> str(type.convert(c(as.character(1:5), "a")))
 Factor w/ 6 levels "1","2","3","4",..: 1 2 3 4 5 6

Bottom line, Arup needs to go back and review his source data to
understand why the columns that should be numeric end up as factors.


Marc Schwartz

on 02/06/2009 07:43 AM Wacek Kusnierczyk wrote:
> if you use read.table for the import, reading about the colClasses
> parameter in ?read.table may help.
> vQ
>> On Thu, 5 Feb 2009 22:50:38, <arup.pramanik27 at gmail.com> wrote:
>>> I am importing a dataset in R where some of the variable are numerical
>> and
>>> some of them are character...but the problem is that R is treating
>>> numerical variables as character (I am using "is.character" to judge the
>>> type). Now the question is how can I convert these character variables
>> into
>>> numeric and also let me know about the other conversion like "numeric to
>>> categorical","numeric to continuous" etc.Thank you.
>>> Arup

More information about the R-help mailing list