[R] nominal to numeric function

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Wed Aug 12 20:16:03 CEST 2009


another approach may be:

foo <- c("blue", "red", "green")
s.foo <- sample(foo, 20, TRUE)
s.foo
match(s.foo, foo)


I hope it helps.

Best,
Dimitris


Noah Silverman wrote:
> 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.
> 

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014




More information about the R-help mailing list