Skip to content

How to convert Charagter variables into numeric

2 messages · Arup, John Fox

#
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
#
Dear Arup,

It's very difficult to know what's going on without more information. What
is the form of the data that you're importing and how are you reading the
data? I ask because the most common way of getting data into R is probably
via read.table(), which by default converts character data to factors but
reads numbers as numeric. If a "column" of input data has one or more
non-numeric entries (other than the default missing-data indicator NA), it
will be read as a factor, even if most entries in the column are numbers.

To answer your more general question, R has a number of "coercion" functions
prefixed by "as." for changing data from one form to another: For example,
if by "numeric to categorical" you mean numeric to a factor, then
as.factor() will do the trick. There is also the more general coercion
function as() (see ?as).

I hope this helps,
 John

------------------------------
John Fox, Professor
Department of Sociology
McMaster University
Hamilton, Ontario, Canada
web: socserv.mcmaster.ca/jfox
On
into
http://www.nabble.com/How-to-convert-Charagter-
http://www.R-project.org/posting-guide.html