[R] How to convert Charagter variables into numeric

John Fox jfox at mcmaster.ca
Fri Feb 6 15:08:16 CET 2009


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


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
> Behalf Of Arup
> Sent: February-06-09 1:51 AM
> To: r-help at r-project.org
> Subject: [R] How to convert Charagter variables into numeric
> 
> 
> 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
> --
> View this message in context:
http://www.nabble.com/How-to-convert-Charagter-
> variables-into-numeric-tp21867485p21867485.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at 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.




More information about the R-help mailing list