FAQ

[R] How to convert Charagter variables into numeric

Arup
Feb 6, 2009 at 6:50 am
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.
reply

Search Discussions

4 responses

  • Karina Knaus at Feb 6, 2009 at 12:05 pm
    You might want to try as.numeric(X), if you want to convert a factor then
    you could use as.numeric(as.character(X)), where X is a "variable".

    Karina




    On Thu, 5 Feb 2009 22:50:38, 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
    --
  • Wacek Kusnierczyk at Feb 6, 2009 at 1:43 pm
    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, 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
  • Marc Schwartz at Feb 6, 2009 at 2:07 pm
    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.

    HTH,

    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, 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
  • John Fox at Feb 6, 2009 at 2:08 pm
    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.

Related Discussions

Discussion Navigation
viewthread | post