[R] nominal to numeric function

Daniel Malter daniel at umd.edu
Wed Aug 12 22:41:07 CEST 2009


foo <- c("blue", "red", "green")
foo=as.factor(foo)
foo=as.numeric(as.character(foo))
foo

#the numeric ordering is alphabetic

hth,
daniel

-------------------------
cuncta stricte discussurus
-------------------------

-----Ursprüngliche Nachricht-----
Von: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Im
Auftrag von Noah Silverman
Gesendet: Wednesday, August 12, 2009 2:09 PM
Cc: r-help at r-project.org
Betreff: Re: [R] nominal to numeric function

Hi,

Thanks for the tip,

Neither method works as my data is truly nominal

-------------------------------------
foo <- c("blue", "red", "green")
as.numeric(foo)
[1] NA NA NA
Warning message:
NAs introduced by coercion
------------------------------------

Rapid Miner has a function that will automatically create an "index" of the
values and create a new variable (or replace the existing).  It also has a
second function that will break the nominal labels into n variable that are
binary:

i.e.

red(0,1)
blue(0,1)
green(0,1)


Or,  I guess I could go back to the source that generates my data and
institute a numeric key for the nominal items.  That seems like the longest
way, but probably the safest to get what I want.

-N



On 8/12/09 9:10 AM, Phil Spector wrote:
> It's generally safer to use
>
>        as.numeric(as.character(variablename))
>
> since it eliminates problems associated with factors.
>
>                                        - Phil Spector
>                      Statistical Computing Facility
>                      Department of Statistics
>                      UC Berkeley
>                      spector at stat.berkeley.edu
>
>
> On Wed, 12 Aug 2009, Daniel Malter wrote:
>
>>
>> Hi you can use newvariable=as.numeric(variablename). This converts 
>> your factors into numeric variables, but not always with the desired 
>> result. So make sure that you check whether "newvariable" gives you 
>> what you want.
>> Otherwise recoding by hand is indicated.
>>
>> Best,
>> Daniel
>>
>>
>>
>> Noah Silverman-3 wrote:
>>>
>>> Hi,
>>>
>>> I'm training an SVM (C-classification from e1071 library)
>>>
>>> Some of the variables in my data set are nominal.  Is there some 
>>> easy/automatic way to convert them to numerical representations?
>>>
>>> Thanks,
>>>
>>> -N
>>>
>>> ______________________________________________
>>> 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.
>>>
>>>
>>
>> --
>> View this message in context: 
>> http://www.nabble.com/nominal-to-numeric-function-tp24930466p24939723
>> .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.
>>
>
> ______________________________________________
> 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.

______________________________________________
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